-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet82.cpp
More file actions
53 lines (52 loc) · 1.42 KB
/
Copy pathleet82.cpp
File metadata and controls
53 lines (52 loc) · 1.42 KB
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
/**
* 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) {
if(head == NULL || head->next == NULL)
return head ;
ListNode* curr = head->next ;
int nval = head->val ;
ListNode* st = NULL ;
ListNode* prev = head ;
ListNode* pprev = NULL ;
int state = 0 ;
while(curr != NULL){
if(curr->val == nval){
if(state == 0){
st = pprev ;
state = 1 ;
}
if(curr->next == NULL){
if(st == NULL)
return NULL ;
else
st->next = NULL ;
}
}else{
if(state == 1){
if(st == NULL)
head = curr ;
else
st->next = curr ;
state = 0 ;
pprev = st ;
}else{
if(pprev == NULL)
pprev = head ;
else
pprev = pprev->next ;
}
nval = curr->val ;
}
curr = curr->next ;
}
return head ;
}
};