-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0083.cpp
More file actions
35 lines (32 loc) · 762 Bytes
/
0083.cpp
File metadata and controls
35 lines (32 loc) · 762 Bytes
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
/**
* 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) { return func1(head); }
ListNode *func1(ListNode *head) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode *curr = head;
ListNode *next = NULL;
while (curr != NULL) {
next = curr->next;
if (next == NULL || next->val != curr->val) {
curr = next;
} else {
while (next->next != NULL && next->next->val == curr->val) {
next = next->next;
}
curr->next = next->next;
curr = curr->next;
}
}
return head;
}
};