Skip to content

Commit 01ca6c1

Browse files
committed
Create Remove Duplicates from Sorted List II.cpp
1 parent f06a8f0 commit 01ca6c1

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *deleteDuplicates(ListNode *head) {
12+
// Start typing your C/C++ solution below
13+
// DO NOT write int main() function
14+
ListNode *h=NULL,*t=NULL;
15+
if (!head)return NULL;
16+
ListNode *ptr=head,*pre=NULL;
17+
while (ptr)
18+
{
19+
if ((!pre||pre->val!=ptr->val)&&(!ptr->next||ptr->next->val!=ptr->val))
20+
{
21+
if (!h)h=t=ptr;
22+
else
23+
{
24+
t->next=ptr;
25+
t=ptr;
26+
}
27+
}
28+
pre=ptr;
29+
ptr=ptr->next;
30+
}
31+
if (t)t->next=NULL; //forgot if (t)
32+
return h;
33+
}
34+
};

0 commit comments

Comments
 (0)