-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_83.cc
40 lines (36 loc) · 1.04 KB
/
question_83.cc
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
/**************************
This problem is asked for solving the sorting linked list who contain the duplicate value,and you should delete them.
My idea is let cur ppointer continuously become next if the pointer value is same as pre pointer until cur -> value is not
same as pre.Then let pre->next = cur.
***************************/
/**
* 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 == nullptr){
return head;
}
ListNode * pre = head;
ListNode * cur = head->next;
while(cur != nullptr){
while(pre->val == cur->val){
cur = cur->next;
if(cur == nullptr){
pre->next = cur;
return head;
}
}
pre->next = cur;
pre = cur;
cur = cur->next;
}
return head;
}
};