-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution2.ts
40 lines (39 loc) · 990 Bytes
/
Solution2.ts
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
/**
* Definition for _Node.
* class _Node {
* val: number
* next: _Node | null
* random: _Node | null
*
* constructor(val?: number, next?: _Node, random?: _Node) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* this.random = (random===undefined ? null : random)
* }
* }
*/
function copyRandomList(head: _Node | null): _Node | null {
if (head === null) {
return null;
}
let cur = head;
while (cur !== null) {
const node = new _Node(cur.val);
node.next = cur.next;
cur.next = node;
cur = node.next;
}
cur = head;
while (cur !== null) {
cur.next.random = cur.random === null ? null : cur.random.next;
cur = cur.next.next;
}
cur = head;
const ans = head.next;
while (cur.next !== null) {
const node = cur.next;
cur.next = node.next;
cur = node;
}
return ans;
}