-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteDuplicates.cpp
More file actions
50 lines (50 loc) · 1.2 KB
/
deleteDuplicates.cpp
File metadata and controls
50 lines (50 loc) · 1.2 KB
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
42
43
44
45
46
47
48
49
50
#include<iostream>
struct ListNode{
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL){}
};
ListNode* solve(ListNode *head){
ListNode *temp = head;
while(temp!=NULL){
if(temp->next && temp->val==temp->next->val){
//then there is duplicates
//1->2->3->3->4
ListNode *ptr = temp->next;
temp->next = ptr->next;
free(ptr);
}
else{
temp = temp->next;
}
//temp = temp->next;
}
return head;
}
int main(){
ListNode *head = new ListNode(1);
ListNode *l0 = new ListNode(1);
ListNode *l1 = new ListNode(2);
ListNode *l2 = new ListNode(3);
ListNode *l3 = new ListNode(4);
ListNode *l4 = new ListNode(4);
ListNode *l5 = new ListNode(6);
ListNode *l6 = new ListNode(7);
ListNode *l7 = new ListNode(7);
ListNode *l8 = new ListNode(7);
head->next = l0;
l0->next = l1;
l1->next = l2;
l2->next - l3;
l3->next = l4;
l4->next = l5;
l5->next = l6;
l6->next = l7;
l7->next = l8;
ListNode *output = solve(head);
while(output){
std::cout<<output->val<<" ";
output = output->next;
}
return 0;
}