-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55_jianzhi_deletedup.cpp
64 lines (61 loc) · 1.64 KB
/
55_jianzhi_deletedup.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
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
/*ListNode* deleteDuplication(ListNode* pHead)//第一种
{
if(pHead == NULL)
return pHead;
ListNode* pre = NULL;
ListNode* cur = pHead;
ListNode* nex = NULL;
ListNode* del = NULL;
while(cur != NULL){
if(cur->next != NULL && cur->next->val == cur->val){
nex = cur->next;
del = nex;
while(nex != NULL && nex->next != NULL && nex->next->val == nex->val){
nex = nex->next;
delete del;
del = nex;
}
if(cur == pHead)
pHead = nex->next;
else
pre->next = nex->next;
delete cur;
cur = nex->next;
delete nex;
}
else{
pre = cur;
cur = cur->next;
}
}
return pHead;
}*/
ListNode* deleteDuplication(ListNode* pHead){//递归
if(pHead == NULL || pHead->next == NULL)
return pHead;
ListNode* cur;
if(pHead->val == pHead->next->val){
cur = pHead->next->next;
while(cur != NULL && cur->val == pHead->val)
cur = cur->next;
return deleteDuplication(cur);
}
else
{
cur = pHead->next;
pHead->next = deleteDuplication(cur);
return pHead;
}
}
};