LeeCode-Remove Duplicates from Sorted List
2015-07-20 09:18
537 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
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; }
相关文章推荐
- A Simple MVVM Example[Forward]
- Android:EditText属性大全
- Vim技巧之四大模式_可视模式
- 如何清除word复制过来的文字背景色的好方法?
- LeeCode-Single Number II
- random walk DEMO
- javascript常用正则表达式和解析
- 十五天精通WCF——第五天 你需要了解的三个小技巧
- 酷我秀场 去掉水印的方法
- LeeCode-Majority Element
- L - Subway - POJ 2502
- hdoj 1564 Play a game
- Vim技巧之四大模式_插入模式
- 理解inode
- sourcetree的使用
- codeforces #262 DIV2 C称号Present(二分法+贪婪)
- LeeCode-Invert Binary Tree
- oracle connect by用法
- iOS开发 偷懒小技巧---自定义XCode代码片段
- 折价买分级基金的教程