-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path83_RemoveDuplicatesFromSortedList.cpp
58 lines (52 loc) · 1.4 KB
/
83_RemoveDuplicatesFromSortedList.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
/*
* @Author: [email protected]
* @Last Modified time: 2016-04-29 16:32:57
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/*
Note about freeing memory.
We need to free memory when we delete a node. But don't use "delete node"!
construct on an interview without discussing it with the interviewer.
A list node can be allocated in many different ways and we can use delete node;
only if we are sure that the nodes were allocated with new TreeNode(...).
We can create LinkList in stack as followings:
ListNode node1(2), node2(2), node3(2), node4(5), node5(7), node6(10);
node1.next = &node2; node2.next = &node3; ....
ListNode* p = Solution().deleteDuplicates(&node1);
*/
// Recursively
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL || head->next==NULL) return head;
head->next = deleteDuplicates(head->next);
return head->val == head->next->val ? head->next : head;
}
};
// Iteratively
class Solution_2 {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* cur=head;
while(cur){
while(cur->next && cur->val==cur->next->val){
cur->next = cur->next->next;
}
cur = cur->next;
}
return head;
}
};
/*
[]
[1]
[3,3,3,3,3]
[1,1,1,2,3,4,4,4,4,5]
*/