File tree 3 files changed +97
-0
lines changed
solution/0100-0199/0138.Copy List with Random Pointer
3 files changed +97
-0
lines changed Original file line number Diff line number Diff line change @@ -379,6 +379,40 @@ var copyRandomList = function (head) {
379
379
};
380
380
```
381
381
382
+ ### ** TypeScript**
383
+
384
+ ``` ts
385
+ /**
386
+ * Definition for Node.
387
+ * class Node {
388
+ * val: number
389
+ * next: Node | null
390
+ * random: Node | null
391
+ * constructor(val?: number, next?: Node, random?: Node) {
392
+ * this.val = (val===undefined ? 0 : val)
393
+ * this.next = (next===undefined ? null : next)
394
+ * this.random = (random===undefined ? null : random)
395
+ * }
396
+ * }
397
+ */
398
+
399
+ function copyRandomList(head : Node | null ): Node | null {
400
+ const map = new Map <Node , Node >();
401
+ let cur = head ;
402
+ while (cur != null ) {
403
+ map .set (cur , new Node (cur .val ));
404
+ cur = cur .next ;
405
+ }
406
+ cur = head ;
407
+ while (cur != null ) {
408
+ map .get (cur ).next = map .get (cur .next ) ?? null ;
409
+ map .get (cur ).random = map .get (cur .random ) ?? null ;
410
+ cur = cur .next ;
411
+ }
412
+ return map .get (head );
413
+ }
414
+ ```
415
+
382
416
### ** ...**
383
417
384
418
```
Original file line number Diff line number Diff line change @@ -349,6 +349,40 @@ var copyRandomList = function (head) {
349
349
};
350
350
```
351
351
352
+ ### ** TypeScript**
353
+
354
+ ``` ts
355
+ /**
356
+ * Definition for Node.
357
+ * class Node {
358
+ * val: number
359
+ * next: Node | null
360
+ * random: Node | null
361
+ * constructor(val?: number, next?: Node, random?: Node) {
362
+ * this.val = (val===undefined ? 0 : val)
363
+ * this.next = (next===undefined ? null : next)
364
+ * this.random = (random===undefined ? null : random)
365
+ * }
366
+ * }
367
+ */
368
+
369
+ function copyRandomList(head : Node | null ): Node | null {
370
+ const map = new Map <Node , Node >();
371
+ let cur = head ;
372
+ while (cur != null ) {
373
+ map .set (cur , new Node (cur .val ));
374
+ cur = cur .next ;
375
+ }
376
+ cur = head ;
377
+ while (cur != null ) {
378
+ map .get (cur ).next = map .get (cur .next ) ?? null ;
379
+ map .get (cur ).random = map .get (cur .random ) ?? null ;
380
+ cur = cur .next ;
381
+ }
382
+ return map .get (head );
383
+ }
384
+ ```
385
+
352
386
### ** ...**
353
387
354
388
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for Node.
3
+ * class Node {
4
+ * val: number
5
+ * next: Node | null
6
+ * random: Node | null
7
+ * constructor(val?: number, next?: Node, random?: Node) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.next = (next===undefined ? null : next)
10
+ * this.random = (random===undefined ? null : random)
11
+ * }
12
+ * }
13
+ */
14
+
15
+ function copyRandomList ( head : Node | null ) : Node | null {
16
+ const map = new Map < Node , Node > ( ) ;
17
+ let cur = head ;
18
+ while ( cur != null ) {
19
+ map . set ( cur , new Node ( cur . val ) ) ;
20
+ cur = cur . next ;
21
+ }
22
+ cur = head ;
23
+ while ( cur != null ) {
24
+ map . get ( cur ) . next = map . get ( cur . next ) ?? null ;
25
+ map . get ( cur ) . random = map . get ( cur . random ) ?? null ;
26
+ cur = cur . next ;
27
+ }
28
+ return map . get ( head ) ;
29
+ }
You can’t perform that action at this time.
0 commit comments