-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCopyLLWithRandomPointer
More file actions
356 lines (266 loc) · 7.67 KB
/
CopyLLWithRandomPointer
File metadata and controls
356 lines (266 loc) · 7.67 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
************************************ I. Using Intelligent Algorithm *****************************************
/*
* Question: Given a LL with next and random pointers which point to any other node in LL
* Devise a algorithm to copy this LL
*
* Source: http://www.careercup.com/question?id=5412018236424192
*
* Algorithm:
*
*
I. Using Intelligent Algorithm:
Node* CopyLinkedList(Node* head) {
Node* n = head;
// First pass, insert a copy next to the code.
while (n) {
Node* n1 = new Node();
n1->data = n->data;
n1->next = n->next;
n->next = n1;
n = n->next->next;
}
// Second pass, set random pointer.
n = head;
while (n) {
n->next->random = n->random->next;
n = n->next->next;
}
// Third pass, detach two lists.
n = head;
Node* head1 = n->next;
while (n) {
Node* temp = n->next->next;
n->next->next = temp ? temp->next : nullptr;
n->next = temp;
n = temp;
}
return head1;
}
II. Using HashMap
A good linear way is to use a map.
1) Go through your linked list, and for each node v, create a new node u, such that u->data = v->data.
Now store (v,u) in your map.
{Note, doesn't matter where you set the u->next, just leave the new nodes dangling in the air}
2) Now go through your original linked list again, and visit every v.
Now do lookups in the map for every v in your original linked list....
Look up v to get u.
Call v->next as x
Call v->random as y
Now set u->next->map[x], u->random=map[y] .
CONS OF USING HASHMAP:
hash maps need something to hash on.. what are you assuming that is?
The value of the node? What if several nodes have the same value?
Your map is going to get messed up pretty quick
VERY IMP NOTE:
HENCE, HASHMAP CAN ONLY BE USED IF EVERY NODE.DATA IS UNIQUE WHEREAS THE FIRST APPROACH
OF USING INTELLIGENT ALGORITHM CAN BE USED FOR ANY CASE (EVEN IF NODE.DATA IS NOT UNIQUE)
*/
package LLCopyWithRandomPointer;
public class UsingInteligentAlgorithms {
public static void main(String[] args) {
// create the head node
Node head=new Node(10);
// create the LL
head.appendToTail(20).appendToTail(30).appendToTail(40).appendToTail(50);
// set the random pointers
head.random=head.next.next.next; //10.random = 40
head.next.random = head; // 20.random=10
head.next.next.random = head.next.next.next.next; //30.random=50
head.next.next.next.random = head.next; // 40.random=20
head.next.next.next.next.random = head.next.next; //50.random = 30
// print original LL
System.out.println("Original LL: ");
printLL(head);
// copy the linkedlist
Node head1 = copyLLWithRandomPointer(head);
// print the copied linked list
System.out.println("Copied LL: ");
printLL(head1);
}
private static Node copyLLWithRandomPointer(Node head) {
// First pass, insert a copy next to the code. Example: a a' b b' c c' NULL
Node n = head;
while(n!=null){
Node copy = new Node(n.data);
copy.next = n.next;
n.next = copy;
// traverse through the LL
n=n.next.next;
}
// Second pass, set random pointer.
n=head;
while(n!=null){
n.next.random = n.random.next;
// traverse through the LL
n=n.next.next;
}
// Third pass, detach two lists.
n = head;
Node head1 = n.next;
while(n!=null){
Node temp = n.next.next;
if(temp!=null)
n.next.next = temp.next;
else
n.next.next = null;
n.next = temp;
// traverse through the LL
n=temp;
}
return head1;
}
private static void printLL(Node head) {
Node n = head;
while(n!=null){
System.out.println("Data:"+n.data+" Random:"+n.random.data);
n=n.next;
}
System.out.println();
}
}
class Node{
public int data;
public Node next;
public Node random;
public Node(int data){
this.data=data;
this.next=null;
this.random=null;
}
public Node appendToTail(int data){
Node n = this;
Node prev = null;
while(n!=null){
prev=n;
n=n.next;
}
prev.next=new Node(data);
return prev.next;
}
}
/*
* Analysis:
* Time Complexity: we run 3 passes hence 3n, asymptotically it is O(n)
* Space Complexity: O(1)
*/
************************************ II. Using HashMap *****************************************
/*
* Question: Given a LL with next and random pointers which point to any other node in LL
* Devise a algorithm to copy this LL
*
* Source: http://www.careercup.com/question?id=5412018236424192
*
* Algorithm:
*
*
I. Using Intelligent Algorithm:
Node* CopyLinkedList(Node* head) {
Node* n = head;
// First pass, insert a copy next to the code.
while (n) {
Node* n1 = new Node();
n1->data = n->data;
n1->next = n->next;
n->next = n1;
n = n->next->next;
}
// Second pass, set random pointer.
n = head;
while (n) {
n->next->random = n->random->next;
n = n->next->next;
}
// Third pass, detach two lists.
n = head;
Node* head1 = n->next;
while (n) {
Node* temp = n->next->next;
n->next->next = temp ? temp->next : nullptr;
n->next = temp;
n = temp;
}
return head1;
}
II. Using HashMap
A good linear way is to use a map.
1) Go through your linked list, and for each node v, create a new node u, such that u->data = v->data.
Now store (v,u) in your map.
{Note, doesn't matter where you set the u->next, just leave the new nodes dangling in the air}
2) Now go through your original linked list again, and visit every v.
Now do lookups in the map for every v in your original linked list....
Look up v to get u.
Call v->next as x
Call v->random as y
Now set u->next->map[x], u->random=map[y] .
CONS OF USING HASHMAP:
hash maps need something to hash on.. what are you assuming that is?
The value of the node? What if several nodes have the same value?
Your map is going to get messed up pretty quick
VERY IMP NOTE:
HENCE, HASHMAP CAN ONLY BE USED IF EVERY NODE.DATA IS UNIQUE WHEREAS THE FIRST APPROACH
OF USING INTELLIGENT ALGORITHM CAN BE USED FOR ANY CASE (EVEN IF NODE.DATA IS NOT UNIQUE)
*/
package LLCopyWithRandomPointer;
import java.util.HashMap;
public class UsingHashMap {
public static void main(String[] args) {
// create the head node
Node head=new Node(10);
// create the LL
head.appendToTail(20).appendToTail(30).appendToTail(40).appendToTail(50);
// set the random pointers
head.random=head.next.next.next; //10.random = 40
head.next.random = head; // 20.random=10
head.next.next.random = head.next.next.next.next; //30.random=50
head.next.next.next.random = head.next; // 40.random=20
head.next.next.next.next.random = head.next.next; //50.random = 30
// print original LL
System.out.println("Original LL: ");
printLL(head);
// copy the linkedlist
Node head1 = usingHashMap(head);
// print the copied linked list
System.out.println("Copied LL: ");
printLL(head1);
}
private static Node usingHashMap(Node head) {
/* VERY IMP NOTE:
HENCE, HASHMAP CAN ONLY BE USED IF EVERY NODE.DATA IS UNIQUE WHEREAS THE FIRST APPROACH
OF USING INTELLIGENT ALGORITHM CAN BE USED FOR ANY CASE (EVEN IF NODE.DATA IS NOT UNIQUE)
*/
HashMap<Node,Node> map = new HashMap<Node,Node>();
// First Pass: Put all the nodes in the HashMap
Node n = head;
while(n!=null){
map.put(n,new Node(n.data));
// iterate through the LL
n=n.next;
}
// Second Pass: Copy the next and random pointers
n = head;
Node head1 = map.get(n);
while(n!=null){
Node copy = map.get(n);
Node next = n.next;
Node random = n.random;
copy.next = map.get(next);
copy.random = map.get(random);
// iterate through the LL
n=n.next;
}
return head1;
}
private static void printLL(Node head) {
Node n = head;
while(n!=null){
System.out.println("Data:"+n.data+" Random:"+n.random.data);
n=n.next;
}
System.out.println();
}
}
/*
* Analysis:
* Time Complexity: we run 2 passes hence 2n, asymptotically it is O(n)
* Space Complexity: O(n), used by HashMap
*/