File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -280,6 +280,52 @@ public class Solution {
280
280
281
281
### ** JavaScript**
282
282
283
+ - 哈希表
284
+
285
+ ``` js
286
+ /**
287
+ * // Definition for a Node.
288
+ * function Node(val, next, random) {
289
+ * this.val = val;
290
+ * this.next = next;
291
+ * this.random = random;
292
+ * };
293
+ */
294
+
295
+ /**
296
+ * @param {Node} head
297
+ * @return {Node}
298
+ */
299
+ var copyRandomList = function (head ) {
300
+ if (head == null ) {
301
+ return head
302
+ }
303
+
304
+ let cur = head
305
+ const map = new Map ();
306
+ while (cur != null ) {
307
+ map .set (cur, new Node (cur .val ))
308
+ cur = cur .next
309
+ }
310
+
311
+ const res = new Node ();
312
+ let newCur = res
313
+ cur = head
314
+ while (cur != null ) {
315
+ const node = map .get (cur)
316
+ node .random = map .get (cur .random )
317
+ newCur .next = node
318
+ newCur = node
319
+ cur = cur .next
320
+ }
321
+
322
+ return res .next
323
+ };
324
+
325
+ ```
326
+
327
+ - 原地算法
328
+
283
329
``` js
284
330
/**
285
331
* // Definition for a Node.
You can’t perform that action at this time.
0 commit comments