forked from sunstick/code-street
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_dup_list.cpp
More file actions
39 lines (33 loc) · 845 Bytes
/
remove_dup_list.cpp
File metadata and controls
39 lines (33 loc) · 845 Bytes
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
/*
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (!head) return head;
ListNode *node = head;
ListNode *next = head -> next;
while (next) {
if (node -> val == next -> val) {
node -> next = next -> next;
delete next;
next = node -> next;
} else {
node = node -> next;
next = next -> next;
}
}
return head;
}
};