forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove_Duplicates_from_Sorted_List_II.cc
More file actions
43 lines (43 loc) · 1.15 KB
/
Remove_Duplicates_from_Sorted_List_II.cc
File metadata and controls
43 lines (43 loc) · 1.15 KB
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
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode **pCur = &head;
ListNode **helper = pCur;
ListNode *entry = NULL;
ListNode *dummy = NULL;
bool flag;
while (*pCur != NULL) {
entry = *pCur;
flag = false;
helper = &(entry->next);
while (*helper != NULL) {
dummy = *helper;
if (dummy->val != entry->val) {
break;
}
flag = true;
*helper = dummy->next;
delete dummy;
dummy = NULL;
}
if (flag) {
*pCur = entry->next;
delete entry;
entry = NULL;
} else {
pCur = &(entry->next);
}
}
return head;
}
};