LeeCode-Remove Duplicates from Sorted List
2016-04-25 18:55
344 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head)
{
if(head==NULL)
return NULL;
if(head!=NULL&&head->next==NULL)
return head;
if(head->next->next==NULL)
{
if(head->val==head->next->val)
{
head->next==NULL;
return head->next;
}
if(head->val!=head->next->val)
{
return head;
}
}
struct ListNode* p;
p=head;
int count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
int *array;
array=(int *)malloc(count*sizeof(int));
p=head;
int i=0;
while(p!=NULL)
{
array[i]=p->val;
i++;
p=p->next;
}
p=head;
for(i=0;i<count-1;i++)
{
if(array[i]!=array[i+1])
{
p->val= array[i];
p=p->next;
}
}
p->val=array[count-1];
p->next=NULL;
return head;
}
For example,
Given
1->1->2, return
1->2.
Given
1->1->2->3->3, return
1->2->3.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head)
{
if(head==NULL)
return NULL;
if(head!=NULL&&head->next==NULL)
return head;
if(head->next->next==NULL)
{
if(head->val==head->next->val)
{
head->next==NULL;
return head->next;
}
if(head->val!=head->next->val)
{
return head;
}
}
struct ListNode* p;
p=head;
int count=0;
while(p!=NULL)
{
p=p->next;
count++;
}
int *array;
array=(int *)malloc(count*sizeof(int));
p=head;
int i=0;
while(p!=NULL)
{
array[i]=p->val;
i++;
p=p->next;
}
p=head;
for(i=0;i<count-1;i++)
{
if(array[i]!=array[i+1])
{
p->val= array[i];
p=p->next;
}
}
p->val=array[count-1];
p->next=NULL;
return head;
}
相关文章推荐
- 对自定义UITableViewCell的理解
- 线程的基本操作——Java线程的创建
- 关于在Fragment中使用ToolBar导致菜单错乱----惊现巨坑
- 一个数组实现两个栈
- 线程同步(条件变量、信号量)以及死锁
- specgram python
- mysql禁用autocommit
- LeeCode-Single Number II
- LCD驱动中pixclock的计算
- SparkSQL toDF() java.lang.NoSuchMethodError: scala.reflect.api.JavaUniverse.runtimeMirror(Ljava/la
- 两个队列实现一个栈
- 欢迎使用CSDN-markdown编辑器
- Java_语言基础
- java面对大数据 并发时候的分治思想及继承的高级运用
- 关于Message目录的设定
- LeeCode-Majority Element
- HTML5属性data-*的操作
- HDU 3829 二分图
- HTML5属性data-*的操作
- 用lsof统计程序打开的句柄数量