-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
88 lines (78 loc) · 1.44 KB
/
linked_list.cpp
File metadata and controls
88 lines (78 loc) · 1.44 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *next;
};
node * head = NULL;
void add(int data){
node * new_node = new node;
new_node->data=data;
new_node->next=NULL;
if(head==NULL){
head=new_node;
}
else{
node * runner = head;
while(runner->next!=NULL)
runner=runner->next;
runner->next=new_node;
}
cout<<"Node inserted \n";
}
void delll(int val){
node * temp=head;
node * prev=temp;
if(temp->data==val){
// delete head node
head=temp->next;
}
else{
while(temp->data!=val){
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
}
free(temp);
free(prev);
cout<<"Deleted\n";
}
void delduplicates(){
node * curr = head;
node * prev=curr;
unordered_set<int>s;
while(curr){
if(s.find(curr->data)!=s.end()){
// node exist delete that node
prev->next=curr->next;
}
else{
s.insert(curr->data);
prev=curr;
}
curr= curr->next;
}
}
void printll(){
node * runner = head;
while(runner){
cout<<runner->data<<"->";
runner=runner->next;
}
cout<<endl;
}
int main(){
add(3);
add(4);
add(5);
add(3);
add(7);
add(4);
add(7);
printll();
//delll(3);
delduplicates();
printll();
return 0;
}