-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57.删除链表中重复的结点.cpp
More file actions
38 lines (37 loc) · 926 Bytes
/
57.删除链表中重复的结点.cpp
File metadata and controls
38 lines (37 loc) · 926 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
36
37
38
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead){
ListNode *newHead = new ListNode(0);
ListNode *pre = newHead;
ListNode *p = pHead;
while(p != NULL){
while(p != NULL && p->next != NULL && p->val == p->next->val){
int value = p->val;
while(p != NULL && p->val == value){
ListNode *toBeDel = p;
p = p->next;
delete toBeDel;
toBeDel = NULL;
}
}
pre->next = p;
if(p != NULL){
p = p->next;
pre = pre->next;
}
}
p = newHead->next;
delete newHead;
newHead = NULL;
return p;
}
};