51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
+ 首先,遍历链表,完成对每个旧节点的复制。
55
+
56
+ ``` bash
57
+ A -> B -> C -> D -> null
58
+
59
+ =>
60
+
61
+ A -> A1 -> B -> B1 -> C -> C1 -> D -> D1 -> null
62
+ ```
63
+
64
+ 接着设置新节点的 ramdom 指针。
65
+
66
+ 然后遍历链表,修改旧节点和新节点的指向,将旧节点指向下一个旧节点,而新节点指向下一个新节点。
67
+
68
+ 最后返回第一个新节点即可。
69
+
54
70
<!-- tabs:start -->
55
71
56
72
### ** Python3**
@@ -66,23 +82,31 @@ class Node:
66
82
self.next = next
67
83
self.random = random
68
84
"""
85
+
69
86
class Solution :
70
87
def copyRandomList (self , head : ' Node' ) -> ' Node' :
71
- if not head:
88
+ if head is None :
72
89
return None
73
- copy_head = Node(- 1 )
74
- cur, t = copy_head, head
75
- cache = {}
76
- while t:
77
- cur.next = Node(t.val)
78
- cache[t] = cur.next
79
- cur, t = cur.next, t.next
80
- t, cur = head, copy_head.next
81
- while t:
82
- cur.random = cache.get(t.random)
83
- cur, t = cur.next, t.next
84
- return copy_head.next
85
90
91
+ cur = head
92
+ while cur:
93
+ node = Node(cur.val, cur.next)
94
+ cur.next = node
95
+ cur = node.next
96
+
97
+ cur = head
98
+ while cur:
99
+ cur.next.random = None if cur.random is None else cur.random.next
100
+ cur = cur.next.next
101
+
102
+ copy = head.next
103
+ cur = head
104
+ while cur:
105
+ next = cur.next
106
+ cur.next = next .next
107
+ next .next = None if next .next is None else next .next.next
108
+ cur = cur.next
109
+ return copy
86
110
```
87
111
88
112
### ** Java**
@@ -104,29 +128,139 @@ class Node {
104
128
}
105
129
}
106
130
*/
131
+
107
132
class Solution {
108
133
public Node copyRandomList (Node head ) {
109
134
if (head == null ) {
110
135
return null ;
111
136
}
112
- Map<Node , Node > map = new HashMap<> ();
113
- Node copyHead = new Node (- 1 );
114
- Node cur = copyHead, t = head;
115
- while (t != null ) {
116
- Node node = new Node (t. val);
117
- map. put(t, node);
137
+
138
+ Node cur = head;
139
+ while (cur != null ) {
140
+ Node node = new Node (cur. val);
141
+ node. next = cur. next;
118
142
cur. next = node;
119
- cur = node;
120
- t = t. next;
143
+ cur = node. next;
121
144
}
122
- cur = copyHead. next;
123
- while (head != null ) {
124
- cur. random = map. get(head. random);
145
+
146
+ cur = head;
147
+ while (cur != null ) {
148
+ cur. next. random = cur. random == null ? null : cur. random. next;
149
+ cur = cur. next. next;
150
+ }
151
+
152
+ Node copy = head. next;
153
+ cur = head;
154
+ while (cur != null ) {
155
+ Node next = cur. next;
156
+ cur. next = next. next;
157
+ next. next = next. next == null ? null : next. next. next;
125
158
cur = cur. next;
126
- head = head. next;
127
159
}
128
- return copyHead. next;
160
+ return copy;
161
+ }
162
+ }
163
+ ```
164
+
165
+ ### ** C++**
166
+
167
+ ``` cpp
168
+ /*
169
+ // Definition for a Node.
170
+ class Node {
171
+ public:
172
+ int val;
173
+ Node* next;
174
+ Node* random;
175
+
176
+ Node(int _val) {
177
+ val = _val;
178
+ next = NULL;
179
+ random = NULL;
180
+ }
181
+ };
182
+ */
183
+
184
+ class Solution {
185
+ public:
186
+ Node* copyRandomList(Node* head) {
187
+ if (!head) {
188
+ return nullptr;
189
+ }
190
+ Node* cur = head;
191
+ while (cur) {
192
+ Node* node = new Node(cur->val);
193
+ node->next = cur->next;
194
+ cur->next = node;
195
+ cur = node->next;
196
+ }
197
+
198
+ cur = head;
199
+ while (cur) {
200
+ cur->next->random = cur->random ? cur->random->next : nullptr;
201
+ cur = cur->next->next;
202
+ }
203
+
204
+ Node* copy = head->next;
205
+ cur = head;
206
+ while (cur) {
207
+ Node* next = cur->next;
208
+ cur->next = next->next;
209
+ next->next = next->next ? next->next->next : nullptr;
210
+ cur = cur->next;
211
+ }
212
+ return copy;
213
+ }
214
+ };
215
+ ```
216
+
217
+ ### ** C#**
218
+
219
+ ``` cs
220
+ /*
221
+ // Definition for a Node.
222
+ public class Node {
223
+ public int val;
224
+ public Node next;
225
+ public Node random;
226
+
227
+ public Node(int _val) {
228
+ val = _val;
229
+ next = null;
230
+ random = null;
231
+ }
232
+ }
233
+ */
234
+
235
+ public class Solution {
236
+ public Node CopyRandomList (Node head ) {
237
+ if (head == null ) {
238
+ return null ;
239
+ }
240
+
241
+ Node cur = head ;
242
+ while (cur != null ) {
243
+ Node node = new Node (cur .val );
244
+ node .next = cur .next ;
245
+ cur .next = node ;
246
+ cur = node .next ;
247
+ }
129
248
249
+ cur = head ;
250
+ while (cur != null ) {
251
+ cur .next .random = cur .random == null ? null : cur .random .next ;
252
+ cur = cur .next .next ;
253
+ }
254
+
255
+ Node copy = head .next ;
256
+ cur = head ;
257
+ while (cur != null ) {
258
+ Node next = cur .next ;
259
+ cur .next = next .next ;
260
+ next .next = next .next == null ? null : next .next .next ;
261
+ cur = cur .next ;
262
+ }
263
+ return copy ;
130
264
}
131
265
}
132
266
```
@@ -142,96 +276,94 @@ class Solution {
142
276
* this.random = random;
143
277
* };
144
278
*/
279
+
145
280
/**
146
281
* @param {Node} head
147
282
* @return {Node}
148
283
*/
149
- var copyRandomList = function (head ) {
150
- function copy (node ) {
151
- if (! node) return null ;
152
- if (isRead .get (node)) return isRead .get (node);
153
- let newNode = new Node (node .val );
154
- isRead .set (node, newNode);
155
- newNode .random = copy (node .random );
156
- newNode .next = copy (node .next );
157
- return newNode;
158
- }
159
- let isRead = new Map ();
160
- return copy (head);
284
+ var copyRandomList = function (head ) {
285
+ if (head == null ) {
286
+ return null ;
287
+ }
288
+ let cur = head;
289
+ while (cur != null ) {
290
+ let node = new Node (cur .val , cur .next );
291
+ cur .next = node;
292
+ cur = node .next ;
293
+ }
294
+
295
+ cur = head;
296
+ while (cur != null ) {
297
+ cur .next .random = cur .random == null ? null : cur .random .next ;
298
+ cur = cur .next .next ;
299
+ }
300
+
301
+ let copy = head .next ;
302
+ cur = head;
303
+ while (cur != null ) {
304
+ let next = cur .next ;
305
+ cur .next = next .next ;
306
+ next .next = next .next == null ? null : next .next .next ;
307
+ cur = cur .next ;
308
+ }
309
+ return copy;
161
310
};
162
311
```
163
312
164
313
### ** Go**
165
314
166
315
``` go
316
+ /* *
317
+ * Definition for a Node.
318
+ * type Node struct {
319
+ * Val int
320
+ * Next *Node
321
+ * Random *Node
322
+ * }
323
+ */
324
+
167
325
func copyRandomList (head *Node ) *Node {
168
326
if head == nil {
169
327
return nil
170
328
}
171
- cur , next := head,head
172
- // 完成对当前节点的复制
329
+
330
+ cur := head
173
331
for cur != nil {
174
- next = cur.Next
175
- cur.Next = &Node{
332
+ node := &Node{
176
333
Val: cur.Val ,
177
- Next: next ,
334
+ Next: cur. Next ,
178
335
Random: nil ,
179
336
}
180
- cur = next
337
+ cur.Next = node
338
+ cur = node.Next
181
339
}
182
- // 设置复制节点的random节点
340
+
183
341
cur = head
184
- curCopy := head
185
342
for cur != nil {
186
- next = cur.Next .Next
187
- curCopy = cur.Next
188
343
if cur.Random == nil {
189
- curCopy .Random = nil
344
+ cur. Next .Random = nil
190
345
} else {
191
- curCopy .Random = cur.Random .Next
346
+ cur. Next .Random = cur.Random .Next
192
347
}
193
- cur = next
348
+ cur = cur. Next . Next
194
349
}
195
- res := head.Next
350
+
351
+ copy := head.Next
196
352
cur = head
197
353
for cur != nil {
198
- next = cur.Next .Next
199
- curCopy = cur.Next
200
- cur.Next = next
201
- if next != nil {
202
- curCopy.Next = next.Next
354
+ next := cur.Next
355
+ cur.Next = next.Next
356
+ if (next.Next == nil ) {
357
+ next.Next = nil
203
358
} else {
204
- curCopy .Next = nil
359
+ next .Next = next. Next . Next
205
360
}
206
361
cur = cur.Next
207
362
}
208
- return res
363
+ return copy
209
364
}
210
365
```
211
366
212
- ### ** C++**
213
-
214
- ``` cpp
215
- class Solution {
216
- public:
217
- Node* copyRandomList(Node* head) {
218
- unordered_map<Node* , Node* > m;
219
- Node* p = head;
220
- while (p) {
221
- m[ p] = new Node(p->val);
222
- p = p->next;
223
- }
224
- p = head;
225
- while (p) {
226
- m[ p] ->next = m[ p->next] ;
227
- m[ p] ->random = m[ p->random] ;
228
- p = p->next;
229
- }
230
- return m[ head] ;
231
- }
232
- };
233
- ```
234
-
235
367
### ** ...**
236
368
237
369
```
0 commit comments