forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_duplicates_soreted_linked_list.cpp
More file actions
58 lines (53 loc) · 1.09 KB
/
remove_duplicates_soreted_linked_list.cpp
File metadata and controls
58 lines (53 loc) · 1.09 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
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node* next;
};
struct node* createList(struct node* head, int data){
struct node *tmp=(struct node*)malloc(sizeof(struct node));
tmp->data=data;
tmp->next=NULL;
if(head==NULL){
head=tmp;
return head;
}
struct node* p=head;
while(p->next!=NULL){
p=p->next;
}
p->next=tmp;
return head;
}
void disp(struct node* head){
struct node* p=head;
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
}
void removeDuplicate(struct node* head){
struct node* tmp=head;
while(tmp!=NULL && tmp->next!=NULL){
if(tmp->data==tmp->next->data){
struct node* toRemove=tmp->next;
tmp->next=tmp->next->next;
free(toRemove);
}
else tmp=tmp->next;
}
}
int main(){
struct node* head=NULL;
int n;
cin>>n;
for(int i=0;i<n;i++){
int data;
cin>>data;
head=createList(head, data);
}
disp(head);
cout<<endl;
removeDuplicate(head);
disp(head);
}