Skip to content

Commit e7960ac

Browse files
Merge pull request #199 from suraj0223/master
added remove_duplicates.cpp
2 parents be350b0 + 5adb139 commit e7960ac

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

LinkedList/remove_duplicates.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct node {
5+
int val;
6+
struct node *next = NULL;
7+
};
8+
9+
struct node* remove_duplicates(struct node *Head) {
10+
11+
set<int> s1;
12+
while (Head) {
13+
s1.insert(Head->val);
14+
Head = Head->next;
15+
}
16+
17+
struct node *newhead = NULL;
18+
struct node *prev = NULL;
19+
20+
// insertion of linked list of n values
21+
for(int x : s1) {
22+
23+
struct node *tempnode = new node();
24+
tempnode->val = x;
25+
if(newhead == nullptr) {
26+
newhead = tempnode;
27+
prev = newhead;
28+
} else {
29+
prev->next = tempnode;
30+
prev = tempnode;
31+
}
32+
}
33+
return newhead;
34+
35+
};
36+
int main() {
37+
38+
int n;
39+
cin >> n;
40+
struct node *Head = NULL;
41+
struct node *prev;
42+
43+
// insertion or creation of linked list of n values
44+
while(n--) {
45+
46+
struct node *tempnode = new node();
47+
cin >> tempnode->val;
48+
if(Head == nullptr) {
49+
Head = tempnode;
50+
prev = Head;
51+
} else {
52+
prev->next = tempnode;
53+
prev = tempnode;
54+
}
55+
}
56+
57+
58+
struct node *newhead = remove_duplicates(Head);
59+
60+
while(newhead) {
61+
cout<< newhead->val << " ";
62+
newhead = newhead->next;
63+
}
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)