-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0029.h
39 lines (34 loc) · 848 Bytes
/
Problem0029.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
//
// Created by Fengwei Zhang on 2021/5/28.
//
#ifndef ACWINGSOLUTION_PROBLEM0029_H
#define ACWINGSOLUTION_PROBLEM0029_H
struct ListNode {
int val;
ListNode *next{};
explicit ListNode(int x) : val(x), next(NULL) {}
};
class Problem0029 {
public:
ListNode *deleteDuplication(ListNode *head) {
if (!head || !head->next) {
return head;
}
auto *dummy = new ListNode(-1);
dummy->next = head;
auto p = dummy;
while (p->next) {
auto q = p->next;
while (q->next && p->next->val == q->next->val) {
q = q->next;
}
if (q != p->next) {
p->next = q->next;
} else {
p = q;
}
}
return dummy->next;
}
};
#endif //ACWINGSOLUTION_PROBLEM0029_H