-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathsolution.h
41 lines (39 loc) · 1.03 KB
/
solution.h
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
#include <cstddef>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (head == NULL) return head;
ListNode preHead(0);
preHead.next = head;
ListNode *pre = &preHead;
bool isDuplicate = false;
ListNode *p = head;
while (p->next)
if (p->val == p->next->val) {
ListNode *next = p->next->next;
delete p->next;
p->next = next;
isDuplicate = true;
} else if (isDuplicate) {
ListNode *next = p->next;
delete p;
p = next;
pre->next = p;
isDuplicate = false;
} else {
pre = p;
p = p->next;
}
if (isDuplicate) {
ListNode *next = p->next;
delete p;
pre->next = next;
}
return preHead.next;
}
};