-
Notifications
You must be signed in to change notification settings - Fork 33
/
138-Copy-List-with-Random-Pointer.js
89 lines (73 loc) · 1.67 KB
/
138-Copy-List-with-Random-Pointer.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* https://leetcode.com/problems/copy-list-with-random-pointer/description/
* Difficulty:Medium
*
* A linked list is given such that each node contains an additional random pointer
* which could point to any node in the list or null.
*
* Return a deep copy of the list.
*
*/
// 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) {
print(head);
var p = head;
// A->B->C
// A->A'->B->B'->C->C'
while (p) {
var copy = new RandomListNode(p.label + "'");
copy.next = p.next;
p.next = copy;
p = copy.next;
}
print(head);
// 构造 A' B' C'.random
p = head;
while (p) {
p.next.random = p.random ? p.random.next : null;
p = p.next.next;
}
print(head);
// 构造 copy
var pp = new RandomListNode(0);
var copy = pp;
p = head;
while (p) {
pp.next = p.next;
pp = pp.next;
p.next = pp.next;
p = p.next;
}
print(head);
print(copy.next);
return copy.next;
};
var a = new RandomListNode('A');
var b = new RandomListNode('B');
var c = new RandomListNode('C');
a.next = b;
b.next = c;
a.random = c;
b.random = c;
c.random = a;
function print(h) {
var ls = [];
var rs = [];
while (h) {
ls.push(h.label);
rs.push(h.random ? h.random.label : 'nil');
h = h.next;
}
console.log(ls.join('->'));
console.log(rs.join(' '));
console.log('-------------');
}
console.log(copyRandomList(a));