-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteDuplicates.cpp
More file actions
41 lines (40 loc) · 914 Bytes
/
deleteDuplicates.cpp
File metadata and controls
41 lines (40 loc) · 914 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
40
41
/*
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) {}
* };
*/
// my Solution
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==nullptr)
return nullptr;
ListNode* fast,*slow,*node;
slow=head;
fast=head->next;
while(fast)
{
if(slow->val==fast->val)
{
node=fast;
fast=fast->next;
slow->next=fast;
delete node;
}else
{
slow=slow->next;
fast=fast->next;
}
}
return head;
}
};