-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemove_Duplicates_from_Sorted_List2.cpp
More file actions
49 lines (49 loc) · 1.36 KB
/
Copy pathRemove_Duplicates_from_Sorted_List2.cpp
File metadata and controls
49 lines (49 loc) · 1.36 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/*
O(N)
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode* newHead=NULL;
ListNode* newCurr=NULL;
if(head==NULL)return NULL;
while(head)
{
if(head->next&&head->val==head->next->val)
{
int tmp=head->val;
do
{
ListNode* tmpHead=head;
head=head->next;
delete tmpHead;
}while(head!=NULL&&head->val==tmp);
}
else
{
if(newHead==NULL)
{
newHead=head;
}
else
{
newCurr->next=head;
}
newCurr=head;
head=head->next;
newCurr->next=NULL;
}
}
return newHead;
}
};