We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f06a8f0 commit 01ca6c1Copy full SHA for 01ca6c1
Remove Duplicates from Sorted List II.cpp
@@ -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