-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path82.cpp
76 lines (71 loc) · 1.95 KB
/
82.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
__________________________________________________________________________________________________
8ms
/**
* 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 == nullptr) return nullptr;
auto pprev = &head;
auto prev = head;
auto next = head->next;
int count = 1;
while (next) {
if (next->val != prev->val) {
if (count > 1) {
*pprev = next;
} else {
pprev = &prev->next;
}
prev = next;
count = 1;
} else {
++count;
}
next = next->next;
}
if (count > 1) {
*pprev = nullptr;
}
return head;
}
};
__________________________________________________________________________________________________
8832 kb
/**
* 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* dummy = new ListNode(-1);
dummy -> next = head;
ListNode* curr = dummy;
ListNode *p1 = dummy, *p2 = head;
while (p1 && p2) {
if (p2 -> next && p2 -> val == p2 -> next -> val) {
int duplicate = p2 -> val;
while (p2 && p2 -> val == duplicate) {
p2 = p2 -> next;
}
p1 -> next = p2;
} else {
p1 = p1 -> next;
p2 = p2 -> next;
}
}
return dummy -> next;
}
};
__________________________________________________________________________________________________