remove-duplicates-from-sorted-list I&II——去除链表中重复项
2017-06-23 23:11
375 查看
I、Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.
PS:遍历,而后记录pre,并删除后续重复node
II、
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
删除所有重复项。
PS:循环比较next->val==cur->val,若next跳动则剔除cur至next之间的节点。否则右移left指针。
For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.
PS:遍历,而后记录pre,并删除后续重复node
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *deleteDuplicates(ListNode *head) { 12 ListNode *cur=head,*pre=NULL; 13 while(cur!=NULL){ 14 if(pre==NULL){ 15 pre=cur; 16 cur=cur->next; 17 continue; 18 } 19 ListNode *next=cur->next; 20 if(pre->val==cur->val){ 21 pre->next=next; 22 }else 23 pre=cur; 24 cur=next; 25 } 26 return head; 27 } 28 };
II、
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
删除所有重复项。
PS:循环比较next->val==cur->val,若next跳动则剔除cur至next之间的节点。否则右移left指针。
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode *deleteDuplicates(ListNode *head) { 12 if(head==NULL) return NULL; 13 ListNode h(-1); 14 ListNode *res=&h; 15 res->next=head; 16 ListNode *cur=head,*pre=NULL,*left=res; 17 while(cur!=NULL){ 18 ListNode* next=cur->next; 19 while(next!=NULL&&cur->val==next->val){ 20 next=next->next; 21 } 22 if(next==cur->next){ 23 left=cur; 24 }else{ 25 left->next=next; 26 } 27 cur=next; 28 } 29 return res->next; 30 } 31 };
相关文章推荐
- Remove Duplicates from Sorted List II 链表去除重复值
- [Lintcode]Remove Duplicates from Sorted List II 删除排序链表中的重复数字 II
- 【Leetcode】82 Remove Duplicates from Sorted List II 【指针&链表】
- LeetCode | Remove Duplicates from Sorted List II(删除链表中重复结点2)
- [LeetCode]Merge Two Sorted Lists & Remove Duplicates from Sorted List 两个有序链表合并为一个&删除链表重复元素
- LeetCode-Remove Duplicates from Sorted List II-从有序链表中移除重复元素-链表操作
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- leetcode:Remove Duplicates from Sorted List(去除链表中重复元素)【面试算法题】
- Leetcode Remove Duplicates from Sorted List II 删除链表中重复的元素II
- 112. 删除排序链表中的重复元素(remove-duplicates-from-sorted-list)(c++)----lintcode面试题之链表
- LeetCode刷题笔记(链表):remove-duplicates-from-sorted-list-ii
- LeetCdoe Remove Duplicates from Sorted List II移掉重复链表中的元素
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II (删除排序链表的重复II)
- Remove Duplicates from Sorted List(去除有序链表中的重复元素)
- remove-duplicates-from-sorted-array-ii——去除重复
- Remove Duplicates from Sorted List 去除链表重复数据
- [Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点
- Remove Duplicates from Sorted List II 链表移除重复值@LeetCode
- Remove Duplicates from Sorted List I&&II