1
+ #include < iostream>
2
+ #include < unordered_map>
3
+ using namespace std ;
4
+
5
+ // Node class to create a node
6
+ class Node
7
+ {
8
+ public:
9
+ int data;
10
+ Node *next;
11
+ Node *random;
12
+
13
+ Node (int val)
14
+ {
15
+ data = val;
16
+ next = NULL ;
17
+ random = NULL ;
18
+ }
19
+ };
20
+
21
+ // List class to manage linked list operations
22
+ class List
23
+ {
24
+ public:
25
+ Node *head;
26
+ Node *tail;
27
+
28
+ List ()
29
+ {
30
+ head = tail = NULL ;
31
+ }
32
+
33
+ void push_back (int val) // Add node to the end
34
+ {
35
+ Node *newNode = new Node (val);
36
+
37
+ if (head == NULL ) // Empty linked list
38
+ {
39
+ head = tail = newNode;
40
+ }
41
+ else
42
+ {
43
+ tail->next = newNode;
44
+ tail = newNode;
45
+ }
46
+ }
47
+
48
+ void setRandom (int nodeIndex, int randomIndex) // to set random pointers
49
+ {
50
+ if (!head)
51
+ return ;
52
+
53
+ Node *temp = head;
54
+ Node *randomNode = head;
55
+ int count = 0 ;
56
+
57
+ while (temp && count < nodeIndex)
58
+ {
59
+ temp = temp->next ;
60
+ count++;
61
+ }
62
+
63
+ count = 0 ;
64
+ while (randomNode && count < randomIndex)
65
+ {
66
+ randomNode = randomNode->next ;
67
+ count++;
68
+ }
69
+
70
+ if (temp)
71
+ temp->random = randomNode;
72
+ }
73
+
74
+ Node *copyRandomList (Node *head)
75
+ {
76
+ if (head == NULL ) // base case
77
+ {
78
+ return NULL ;
79
+ }
80
+
81
+ unordered_map<Node *, Node *> m;
82
+
83
+ Node *newHead = new Node (head->data );
84
+ Node *oldTemp = head->next ;
85
+ Node *newTemp = newHead;
86
+ m[head] = newHead;
87
+
88
+ while (oldTemp != NULL ) // plain copy
89
+ {
90
+ Node *copyNode = new Node (oldTemp->data );
91
+ m[oldTemp] = copyNode;
92
+ newTemp->next = copyNode;
93
+
94
+ oldTemp = oldTemp->next ;
95
+ newTemp = newTemp->next ;
96
+ }
97
+
98
+ oldTemp = head;
99
+ newTemp = newHead;
100
+
101
+ while (oldTemp != NULL ) // random connections copy
102
+ {
103
+ newTemp->random = m[oldTemp->random ];
104
+ oldTemp = oldTemp->next ;
105
+ newTemp = newTemp->next ;
106
+ }
107
+
108
+ return newHead;
109
+ }
110
+
111
+ void printLL (Node *head)
112
+ {
113
+ Node *temp = head;
114
+ while (temp != NULL )
115
+ {
116
+ cout << temp->data << " (random: " ;
117
+ if (temp->random )
118
+ cout << temp->random ->data ;
119
+ else
120
+ cout << " NULL" ;
121
+ cout << " ) -> " ;
122
+ temp = temp->next ;
123
+ }
124
+ cout << " NULL" << endl;
125
+ }
126
+ };
127
+
128
+ int main ()
129
+ {
130
+ List ll1;
131
+ ll1.push_back (1 );
132
+ ll1.push_back (2 );
133
+ ll1.push_back (3 );
134
+ ll1.push_back (4 );
135
+ ll1.push_back (5 );
136
+
137
+ // Setting up random pointers
138
+ ll1.setRandom (0 , 2 ); // Node 1 -> Node 3
139
+ ll1.setRandom (1 , 4 ); // Node 2 -> Node 5
140
+ ll1.setRandom (2 , 1 ); // Node 3 -> Node 2
141
+ ll1.setRandom (3 , 3 ); // Node 4 -> Node 4
142
+ ll1.setRandom (4 , 0 ); // Node 5 -> Node 1
143
+
144
+ cout << " Original List:" << endl;
145
+ ll1.printLL (ll1.head );
146
+
147
+ // Copying the linked list
148
+ List ll2;
149
+ ll2.head = ll1.copyRandomList (ll1.head );
150
+
151
+ cout << " Copied List:" << endl;
152
+ ll2.printLL (ll2.head );
153
+
154
+ return 0 ;
155
+ }
0 commit comments