-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
83.cpp
49 lines (47 loc) · 1.25 KB
/
83.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
__________________________________________________________________________________________________
12ms
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *cur = head;
while (cur && cur->next) {
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
} else {
cur = cur->next;
}
}
return head;
}
};
__________________________________________________________________________________________________
8980 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* p = head;
if (p == nullptr) return head;
ListNode* q = p->next;
unordered_set<int> s;
while (q != nullptr) {
if (q->val == p->val) {
q = q->next;
p->next = q;
}
else {
p = q;
q = q->next;
}
}
return head;
}
};
__________________________________________________________________________________________________