-
Notifications
You must be signed in to change notification settings - Fork 0
/
57.LinkedList_removeDuplicates.cpp
68 lines (59 loc) · 1.17 KB
/
57.LinkedList_removeDuplicates.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
using namespace std;
struct node{
int data;
node* next;
};
node* first = new node;
void remove_duplicates(node* p){
node* q=p;
p = p->next;
while(p){
if(p->data != q->data){
q = p;
p = p->next;
} else{
q->next = p->next;
delete p;
p = q->next;
}
}
}
void remove_duplicates_single_pointer(node *p){
while(p->next){
if(p->data!=(p->next)->data){
p = p->next;
} else{
//delete p->next;
p->next = (p->next)->next;
}
}
}
void create(node* p){
int num;
cout<<"enter number of elements in linked list: ";
cin>>num;
cout<<"enter "<<num<<" elements: ";
cin>>p->data;
for(int i=1;i<num;i++){
node* temp = new node;
cin>>temp->data;
temp->next = p->next;
p->next = temp;
p = temp;
}
}
void display(node *p){
cout<<endl;
while(p!=NULL){
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main(){
create(first);
display(first);
remove_duplicates_single_pointer(first);
display(first);
}