Remove Duplicates from Sorted List II
2015-02-26 14:26
204 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
1 需要一个dummy header来让头处理变得简单
2 维护两个指针,一个为当前指针,一个为重复指针:如果重复指针不为当前指针的后一个,那么删掉从当前往后包括重复指针的所有元素。否则向前挪动当前指针。。。
3. 注意的是无论上一题还是这一题在处理重复指针的时候都是用的p->next=p2->next。 这个地方错了两回了。。。sigh...!!!
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* p=new ListNode(INT_MIN);
p->next=head;
head=p;
while(p->next){
ListNode* p2=p->next;
while(p2->next && p2->val==p2->next->val)
p2=p2->next;
if (p->next!=p2){
ListNode* tmp=p->next;
p->next=p2->next;
deleteDup(tmp,p2->next);
} else {
p=p->next;
}
}
ListNode *new_head=head->next;
delete head;
return new_head;
}
void deleteDup(ListNode* beg, ListNode* end){
ListNode* cur=beg;
while(cur!=end){
ListNode* tmp=cur;
cur=cur->next;
delete tmp;
}
}
};
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
1 需要一个dummy header来让头处理变得简单
2 维护两个指针,一个为当前指针,一个为重复指针:如果重复指针不为当前指针的后一个,那么删掉从当前往后包括重复指针的所有元素。否则向前挪动当前指针。。。
3. 注意的是无论上一题还是这一题在处理重复指针的时候都是用的p->next=p2->next。 这个地方错了两回了。。。sigh...!!!
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* p=new ListNode(INT_MIN);
p->next=head;
head=p;
while(p->next){
ListNode* p2=p->next;
while(p2->next && p2->val==p2->next->val)
p2=p2->next;
if (p->next!=p2){
ListNode* tmp=p->next;
p->next=p2->next;
deleteDup(tmp,p2->next);
} else {
p=p->next;
}
}
ListNode *new_head=head->next;
delete head;
return new_head;
}
void deleteDup(ListNode* beg, ListNode* end){
ListNode* cur=beg;
while(cur!=end){
ListNode* tmp=cur;
cur=cur->next;
delete tmp;
}
}
};
相关文章推荐
- LeetCode:Remove Duplicates from Sorted List I II
- [leetcode]Remove Duplicates from Sorted List II @ Python
- [LeetCode]Remove Duplicates from Sorted List II
- LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)
- LeetCode – Refresh – Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II
- LeetCode 82: Remove Duplicates from Sorted List II
- leetcode-Remove Duplicates from Sorted List II
- 【LeetCode】Remove Duplicates from Sorted List I && II
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- 【Leetcode】之Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- leetcode 82-- Remove Duplicates from Sorted List II 链表 双指针 删除元素
- Leetcode Remove Duplicates from Sorted List II
- [LeetCode][Java] Remove Duplicates from Sorted List II
- [C++]LeetCode: 6 Remove Duplicates from Sorted List II
- LeetCode Remove Duplicates from Sorted List II
- [Leetcode]Remove Duplicates from Sorted List II