-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-138-Copy-List-with-Random-Pointer.java
258 lines (208 loc) · 7.49 KB
/
LeetCode-138-Copy-List-with-Random-Pointer.java
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
// public Node copyRandomList(Node head) {
// if(head == null) return head;
// HashMap<Node, Node> map = new HashMap<Node, Node>();
// Node newHead = new Node(head.val);
// Node p = head, q = newHead;
// map.put(head, newHead);
// p = p.next;
// // 1.Copy Nodes
// while(p != null){
// Node temp = new Node(p.label);
// map.put(p, temp);
// q.next = temp;
// q = q.next;
// p = p.next;
// }
// p = head;
// q = newHead;
// // 2.Copy Random Pointer of each node
// while(p != null){
// if(p.random != null){
// q.random = map.get(p.random);
// }else{
// q.random = null;
// }
// p = p.next;
// q = q.next;
// }
// return newHead;
// }
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
// public Node copyRandomList(Node head) {
// if(head == null) return head;
// HashMap<Node, Node> map = new HashMap<Node, Node>();
// Node newHead = new Node(head.val);
// Node p = head, q = newHead;
// map.put(head, newHead);
// p = p.next;
// // 1.Copy Nodes
// while(p != null){
// Node temp = new Node(p.label);
// map.put(p, temp);
// q.next = temp;
// q = q.next;
// p = p.next;
// }
// p = head;
// q = newHead;
// // 2.Copy Random Pointer of each node
// while(p != null){
// if(p.random != null){
// q.random = map.get(p.random);
// }else{
// q.random = null;
// }
// p = p.next;
// q = q.next;
// }
// return newHead;
// }
/*
https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43491/A-solution-with-constant-space-complexity-O(1)-and-linear-time-complexity-O(N)
*/
public Node copyRandomList(Node head) {
if (head == null) return null;
// Step 1. Copy each node, and link them together side by side in a single-linked list.
Node curr = head;
while (curr != null) {
Node next = curr.next;
Node copy = new Node(curr.val);
curr.next = copy;
copy.next = next;
curr = next;
}
// Step 2. assign random pointer to for the copied nodes.
curr = head;
while (curr != null) {
if (curr.random != null) {
curr.next.random = curr.random.next;
}
curr = curr.next.next;
}
// Step 3. Restore the orignal list, and extract the copied list
curr = head;
Node copyDummy = new Node(-1);
Node copyPre = copyDummy;
while (curr != null) {
// Restore the copied list
Node copy = curr.next;
copyPre.next = copy;
copyPre = copy;
// Restore the original list
Node next = copy.next;
curr.next = next;
curr = next;
}
return copyDummy.next;
}
private void printNode(Node node) {
StringBuilder sb = new StringBuilder();
sb.append("val: ").append(node.val);
String next = null;
if (node.next != null) {
next = "" + node.next.val;
}
sb.append(" next: ").append(next);
String random = null;
if (node.random != null) {
random = "" + node.random.val;
}
sb.append(" random: ").append(random);
System.out.println(sb.toString());
}
private void printList(Node head) {
StringBuilder sb = new StringBuilder();
while (head != null) {
int val = -1;
if (head.random != null) {
val = head.random.val;
}
sb.append("[").append(head.val).append(",").append(val).append("],");
head = head.next;
}
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb.toString());
}
/*
My solution doesn't work because if we copy from left to right, and restore from left to right, the random could point to a previous node whose random pointer been restored.
*/
// public Node copyRandomList(Node head) {
// if (head == null) return null;
// Node copyHead = new Node(head.val);
// copyHead.random = head.random;
// head.random = copyHead;
// Node curr = head.next;
// while (curr != null) {
// Node copy = new Node(curr.val);
// copy.random = curr.random;
// curr.random = copy;
// curr = curr.next;
// }
// curr = head;
// while (curr != null) {
// Node copy = curr.random;
// Node random = copy.random;
// // Fix the curr node
// curr.random = random;
// // Fix the copy node
// if (curr.next != null) {
// copy.next = curr.next.random;
// } else {
// copy.next = null;
// }
// if (random != null) {
// copy.random = random.random;
// }
// curr = curr.next;
// }
// return copyHead;
// }
public Node copyRandomList(Node head) {
if (head == null) return null;
// 1. Copy nodes, and chain the nodes (original and copy) in one single linked list.
Node curr = head;
while (curr != null) {
Node copy = new Node(curr.val);
Node next = curr.next;
curr.next = copy;
copy.next = next;
curr = next;
}
// 2. Set the random pointer for copied nodes
curr = head;
while (curr != null) {
Node copy = curr.next;
Node random = curr.random;
if (random != null) copy.random = random.next;
curr = curr.next.next;
}
// 3. Rebuild the two list
Node copyDummy = new Node(-1);
Node prev = copyDummy;
curr = head;
while (curr != null) {
// Fix the copied list
Node copy = curr.next;
prev.next = copy;
prev = copy;
// Fix the original list
Node next = copy.next;
curr.next = next;
curr = next;
}
return copyDummy.next;
}
}