-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-duplicates-from-sorted-list-ii.cpp
44 lines (39 loc) · 1.29 KB
/
remove-duplicates-from-sorted-list-ii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 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) {
if(head==NULL) return NULL;
//先找到第一个符合条件的点 作为根结点
while(head->next!=NULL && head->val==head->next->val){
while(head->next!=NULL && head->val==head->next->val){
head=head->next;
}
if(head!=NULL) head=head->next;
if(head==NULL) return NULL;
}
ListNode* result=head;//根结点
ListNode* valid=head;
if(head) head=head->next;//看看后续的点是否满足条件
while(head){
while(head && head->next!=NULL && head->val==head->next->val){
while(head->next!=NULL && head->val==head->next->val){
head=head->next;
}
if(head!=NULL) head=head->next;
if(head==NULL) head=NULL;
}
valid->next=head;//此时的head一定不是冗余节点
valid=head;
if(head) head=head->next;
}
return result;
}
};