-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmdupinsortedlist2.cpp
47 lines (40 loc) · 1.1 KB
/
rmdupinsortedlist2.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
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode * result = new ListNode(0);
if(head == NULL)
return result->next;
int tag = result->val;
ListNode * pre = result;
int count = 0;
while(head != NULL){
if(head ->val == tag){
count++;
}
else{
if(count == 1){
pre->next = new ListNode(tag);
pre = pre->next;
}
count = 1;
tag = head->val;
}
head = head->next;
}
if(count == 1){
pre->next = new ListNode(tag);
pre = pre->next;
}
return result->next;
}
};