File tree 1 file changed +66
-0
lines changed
1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments