Skip to content

Commit 147e51e

Browse files
authored
83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List
1 parent ff6a14e commit 147e51e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

deleteDuplicates.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// since it is a sorted list, no need to maintain any hash set
2+
class Solution {
3+
public:
4+
ListNode* deleteDuplicates(ListNode* head) {
5+
if(!head) return NULL;
6+
ListNode* temp = head;
7+
while(temp && temp->next){
8+
if(temp->val != temp->next->val) {
9+
temp = temp->next;
10+
} else {
11+
if(temp->next->next) {
12+
temp->next = temp->next->next;
13+
}
14+
else{
15+
temp->next = NULL;
16+
break;
17+
}
18+
}
19+
}
20+
return head;
21+
}
22+
};

0 commit comments

Comments
 (0)