-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
34 lines (30 loc) · 715 Bytes
/
solution.js
File metadata and controls
34 lines (30 loc) · 715 Bytes
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
/**
* Definition for singly-linked list with a random pointer.
* function RandomListNode(label) {
* this.label = label;
* this.next = this.random = null;
* }
*/
/**
* @param {RandomListNode} head
* @return {RandomListNode}
*/
var copyRandomList = function(head) {
let dummy = new RandomListNode(-1),
d = dummy,
p = head,
nodeMap = new Map()
while (p) {
d.next = new RandomListNode(p.label)
nodeMap.set(p, d.next)
p = p.next
d = d.next
}
d = dummy.next
while (head) {
d.random = head.random ? nodeMap.get(head.random) : null
d = d.next
head = head.next
}
return dummy.next
};