-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path082.cpp
40 lines (40 loc) · 1.04 KB
/
082.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
/**
* 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* pre = new ListNode(0);
pre->next = head;
// 使用三个辅助节点
ListNode *first = pre, *second = head, *thrid;
while(second != NULL)
{
thrid = second->next;
if(thrid == NULL) break;
// 发现重复的时候,删除所有重复节点
if(second->val == thrid->val)
{
while(thrid != NULL && second->val == thrid->val)
{
thrid = thrid->next;
second->next = thrid;
}
first->next = thrid;
second = thrid;
}
else
{
first = second;
second = thrid;
}
}
return pre->next;
}
};