Skip to content

Commit 7d6ead8

Browse files
authored
Create 082.Remove-Duplicates-from-Sorted-List-II.cpp
1 parent 9be37ae commit 7d6ead8

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() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* deleteDuplicates(ListNode* head)
14+
{
15+
ListNode* dummy = new ListNode();
16+
dummy -> next = head;
17+
ListNode* h = dummy;
18+
while (h && h->next)
19+
{
20+
ListNode* p = h->next;
21+
int count = 1;
22+
while (p && p->next && p->next->val == p->val)
23+
{
24+
count++;
25+
p = p->next;
26+
}
27+
if (count > 1)
28+
h->next = p->next;
29+
else
30+
h = h->next;
31+
}
32+
return dummy->next;
33+
}
34+
};

0 commit comments

Comments
 (0)