Skip to content

Commit db15933

Browse files
committedJun 3, 2021
feat: add solutions to lc/lcof problem: Copy List with Random Pointer
1 parent 4844a61 commit db15933

File tree

15 files changed

+1207
-207
lines changed

15 files changed

+1207
-207
lines changed
 

‎lcof/面试题35. 复杂链表的复制/README.md

+214-82
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
首先,遍历链表,完成对每个旧节点的复制。
55+
56+
```bash
57+
A -> B -> C -> D -> null
58+
59+
=>
60+
61+
A -> A1 -> B -> B1 -> C -> C1 -> D -> D1 -> null
62+
```
63+
64+
接着设置新节点的 ramdom 指针。
65+
66+
然后遍历链表,修改旧节点和新节点的指向,将旧节点指向下一个旧节点,而新节点指向下一个新节点。
67+
68+
最后返回第一个新节点即可。
69+
5470
<!-- tabs:start -->
5571

5672
### **Python3**
@@ -66,23 +82,31 @@ class Node:
6682
self.next = next
6783
self.random = random
6884
"""
85+
6986
class Solution:
7087
def copyRandomList(self, head: 'Node') -> 'Node':
71-
if not head:
88+
if head is None:
7289
return None
73-
copy_head = Node(-1)
74-
cur, t = copy_head, head
75-
cache = {}
76-
while t:
77-
cur.next = Node(t.val)
78-
cache[t] = cur.next
79-
cur, t = cur.next, t.next
80-
t, cur = head, copy_head.next
81-
while t:
82-
cur.random = cache.get(t.random)
83-
cur, t = cur.next, t.next
84-
return copy_head.next
8590

91+
cur = head
92+
while cur:
93+
node = Node(cur.val, cur.next)
94+
cur.next = node
95+
cur = node.next
96+
97+
cur = head
98+
while cur:
99+
cur.next.random = None if cur.random is None else cur.random.next
100+
cur = cur.next.next
101+
102+
copy = head.next
103+
cur = head
104+
while cur:
105+
next = cur.next
106+
cur.next = next.next
107+
next.next = None if next.next is None else next.next.next
108+
cur = cur.next
109+
return copy
86110
```
87111

88112
### **Java**
@@ -104,29 +128,139 @@ class Node {
104128
}
105129
}
106130
*/
131+
107132
class Solution {
108133
public Node copyRandomList(Node head) {
109134
if (head == null) {
110135
return null;
111136
}
112-
Map<Node, Node> map = new HashMap<>();
113-
Node copyHead = new Node(-1);
114-
Node cur = copyHead, t = head;
115-
while (t != null) {
116-
Node node = new Node(t.val);
117-
map.put(t, node);
137+
138+
Node cur = head;
139+
while (cur != null) {
140+
Node node = new Node(cur.val);
141+
node.next = cur.next;
118142
cur.next = node;
119-
cur = node;
120-
t = t.next;
143+
cur = node.next;
121144
}
122-
cur = copyHead.next;
123-
while (head != null) {
124-
cur.random = map.get(head.random);
145+
146+
cur = head;
147+
while (cur != null) {
148+
cur.next.random = cur.random == null ? null : cur.random.next;
149+
cur = cur.next.next;
150+
}
151+
152+
Node copy = head.next;
153+
cur = head;
154+
while (cur != null) {
155+
Node next = cur.next;
156+
cur.next = next.next;
157+
next.next = next.next == null ? null : next.next.next;
125158
cur = cur.next;
126-
head = head.next;
127159
}
128-
return copyHead.next;
160+
return copy;
161+
}
162+
}
163+
```
164+
165+
### **C++**
166+
167+
```cpp
168+
/*
169+
// Definition for a Node.
170+
class Node {
171+
public:
172+
int val;
173+
Node* next;
174+
Node* random;
175+
176+
Node(int _val) {
177+
val = _val;
178+
next = NULL;
179+
random = NULL;
180+
}
181+
};
182+
*/
183+
184+
class Solution {
185+
public:
186+
Node* copyRandomList(Node* head) {
187+
if (!head) {
188+
return nullptr;
189+
}
190+
Node* cur = head;
191+
while (cur) {
192+
Node* node = new Node(cur->val);
193+
node->next = cur->next;
194+
cur->next = node;
195+
cur = node->next;
196+
}
197+
198+
cur = head;
199+
while (cur) {
200+
cur->next->random = cur->random ? cur->random->next : nullptr;
201+
cur = cur->next->next;
202+
}
203+
204+
Node* copy = head->next;
205+
cur = head;
206+
while (cur) {
207+
Node* next = cur->next;
208+
cur->next = next->next;
209+
next->next = next->next ? next->next->next : nullptr;
210+
cur = cur->next;
211+
}
212+
return copy;
213+
}
214+
};
215+
```
216+
217+
### **C#**
218+
219+
```cs
220+
/*
221+
// Definition for a Node.
222+
public class Node {
223+
public int val;
224+
public Node next;
225+
public Node random;
226+
227+
public Node(int _val) {
228+
val = _val;
229+
next = null;
230+
random = null;
231+
}
232+
}
233+
*/
234+
235+
public class Solution {
236+
public Node CopyRandomList(Node head) {
237+
if (head == null) {
238+
return null;
239+
}
240+
241+
Node cur = head;
242+
while (cur != null) {
243+
Node node = new Node(cur.val);
244+
node.next = cur.next;
245+
cur.next = node;
246+
cur = node.next;
247+
}
129248

249+
cur = head;
250+
while (cur != null) {
251+
cur.next.random = cur.random == null ? null : cur.random.next;
252+
cur = cur.next.next;
253+
}
254+
255+
Node copy = head.next;
256+
cur = head;
257+
while (cur != null) {
258+
Node next = cur.next;
259+
cur.next = next.next;
260+
next.next = next.next == null ? null : next.next.next;
261+
cur = cur.next;
262+
}
263+
return copy;
130264
}
131265
}
132266
```
@@ -142,96 +276,94 @@ class Solution {
142276
* this.random = random;
143277
* };
144278
*/
279+
145280
/**
146281
* @param {Node} head
147282
* @return {Node}
148283
*/
149-
var copyRandomList = function (head) {
150-
function copy(node) {
151-
if (!node) return null;
152-
if (isRead.get(node)) return isRead.get(node);
153-
let newNode = new Node(node.val);
154-
isRead.set(node, newNode);
155-
newNode.random = copy(node.random);
156-
newNode.next = copy(node.next);
157-
return newNode;
158-
}
159-
let isRead = new Map();
160-
return copy(head);
284+
var copyRandomList = function(head) {
285+
if (head == null) {
286+
return null;
287+
}
288+
let cur = head;
289+
while (cur != null) {
290+
let node = new Node(cur.val, cur.next);
291+
cur.next = node;
292+
cur = node.next;
293+
}
294+
295+
cur = head;
296+
while (cur != null) {
297+
cur.next.random = cur.random == null ? null : cur.random.next;
298+
cur = cur.next.next;
299+
}
300+
301+
let copy = head.next;
302+
cur = head;
303+
while (cur != null) {
304+
let next = cur.next;
305+
cur.next = next.next;
306+
next.next = next.next == null ? null : next.next.next;
307+
cur = cur.next;
308+
}
309+
return copy;
161310
};
162311
```
163312

164313
### **Go**
165314

166315
```go
316+
/**
317+
* Definition for a Node.
318+
* type Node struct {
319+
* Val int
320+
* Next *Node
321+
* Random *Node
322+
* }
323+
*/
324+
167325
func copyRandomList(head *Node) *Node {
168326
if head == nil {
169327
return nil
170328
}
171-
cur, next := head,head
172-
//完成对当前节点的复制
329+
330+
cur := head
173331
for cur != nil {
174-
next = cur.Next
175-
cur.Next = &Node{
332+
node := &Node{
176333
Val: cur.Val,
177-
Next: next,
334+
Next: cur.Next,
178335
Random: nil,
179336
}
180-
cur = next
337+
cur.Next = node
338+
cur = node.Next
181339
}
182-
//设置复制节点的random节点
340+
183341
cur = head
184-
curCopy := head
185342
for cur != nil {
186-
next = cur.Next.Next
187-
curCopy = cur.Next
188343
if cur.Random == nil {
189-
curCopy.Random = nil
344+
cur.Next.Random = nil
190345
} else {
191-
curCopy.Random = cur.Random.Next
346+
cur.Next.Random = cur.Random.Next
192347
}
193-
cur = next
348+
cur = cur.Next.Next
194349
}
195-
res := head.Next
350+
351+
copy := head.Next
196352
cur = head
197353
for cur != nil {
198-
next = cur.Next.Next
199-
curCopy = cur.Next
200-
cur.Next = next
201-
if next != nil {
202-
curCopy.Next = next.Next
354+
next := cur.Next
355+
cur.Next = next.Next
356+
if (next.Next == nil) {
357+
next.Next = nil
203358
} else {
204-
curCopy.Next = nil
359+
next.Next = next.Next.Next
205360
}
206361
cur = cur.Next
207362
}
208-
return res
363+
return copy
209364
}
210365
```
211366

212-
### **C++**
213-
214-
```cpp
215-
class Solution {
216-
public:
217-
Node* copyRandomList(Node* head) {
218-
unordered_map<Node*, Node*> m;
219-
Node* p = head;
220-
while (p) {
221-
m[p] = new Node(p->val);
222-
p = p->next;
223-
}
224-
p = head;
225-
while (p) {
226-
m[p]->next = m[p->next];
227-
m[p]->random = m[p->random];
228-
p = p->next;
229-
}
230-
return m[head];
231-
}
232-
};
233-
```
234-
235367
### **...**
236368

237369
```
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* next;
7+
Node* random;
8+
9+
Node(int _val) {
10+
val = _val;
11+
next = NULL;
12+
random = NULL;
13+
}
14+
};
15+
*/
16+
117
class Solution {
218
public:
319
Node* copyRandomList(Node* head) {
4-
unordered_map<Node*, Node*> m;
5-
Node* p = head;
6-
while (p) {
7-
m[p] = new Node(p->val);
8-
p = p->next;
20+
if (!head) {
21+
return nullptr;
922
}
10-
p = head;
11-
while (p) {
12-
m[p]->next = m[p->next];
13-
m[p]->random = m[p->random];
14-
p = p->next;
23+
Node* cur = head;
24+
while (cur) {
25+
Node* node = new Node(cur->val);
26+
node->next = cur->next;
27+
cur->next = node;
28+
cur = node->next;
1529
}
16-
return m[head];
30+
31+
cur = head;
32+
while (cur) {
33+
cur->next->random = cur->random ? cur->random->next : nullptr;
34+
cur = cur->next->next;
35+
}
36+
37+
Node* copy = head->next;
38+
cur = head;
39+
while (cur) {
40+
Node* next = cur->next;
41+
cur->next = next->next;
42+
next->next = next->next ? next->next->next : nullptr;
43+
cur = cur->next;
44+
}
45+
return copy;
1746
}
18-
};
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
// Definition for a Node.
3+
public class Node {
4+
public int val;
5+
public Node next;
6+
public Node random;
7+
8+
public Node(int _val) {
9+
val = _val;
10+
next = null;
11+
random = null;
12+
}
13+
}
14+
*/
15+
16+
public class Solution {
17+
public Node CopyRandomList(Node head) {
18+
if (head == null) {
19+
return null;
20+
}
21+
22+
Node cur = head;
23+
while (cur != null) {
24+
Node node = new Node(cur.val);
25+
node.next = cur.next;
26+
cur.next = node;
27+
cur = node.next;
28+
}
29+
30+
cur = head;
31+
while (cur != null) {
32+
cur.next.random = cur.random == null ? null : cur.random.next;
33+
cur = cur.next.next;
34+
}
35+
36+
Node copy = head.next;
37+
cur = head;
38+
while (cur != null) {
39+
Node next = cur.next;
40+
cur.next = next.next;
41+
next.next = next.next == null ? null : next.next.next;
42+
cur = cur.next;
43+
}
44+
return copy;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
1-
func copyRandomList(head *Node) *Node {
1+
/**
2+
* Definition for a Node.
3+
* type Node struct {
4+
* Val int
5+
* Next *Node
6+
* Random *Node
7+
* }
8+
*/
9+
10+
func copyRandomList(head *Node) *Node {
211
if head == nil {
312
return nil
413
}
5-
cur, next := head,head
6-
//完成对当前节点的复制
14+
15+
cur := head
716
for cur != nil {
8-
next = cur.Next
9-
cur.Next = &Node{
17+
node := &Node{
1018
Val: cur.Val,
11-
Next: next,
19+
Next: cur.Next,
1220
Random: nil,
1321
}
14-
cur = next
22+
cur.Next = node
23+
cur = node.Next
1524
}
16-
//设置复制节点的random节点
25+
1726
cur = head
18-
curCopy := head
1927
for cur != nil {
20-
next = cur.Next.Next
21-
curCopy = cur.Next
2228
if cur.Random == nil {
23-
curCopy.Random = nil
29+
cur.Next.Random = nil
2430
} else {
25-
curCopy.Random = cur.Random.Next
31+
cur.Next.Random = cur.Random.Next
2632
}
27-
cur = next
33+
cur = cur.Next.Next
2834
}
29-
res := head.Next
35+
36+
copy := head.Next
3037
cur = head
3138
for cur != nil {
32-
next = cur.Next.Next
33-
curCopy = cur.Next
34-
cur.Next = next
35-
if next != nil {
36-
curCopy.Next = next.Next
39+
next := cur.Next
40+
cur.Next = next.Next
41+
if (next.Next == nil) {
42+
next.Next = nil
3743
} else {
38-
curCopy.Next = nil
44+
next.Next = next.Next.Next
3945
}
4046
cur = cur.Next
4147
}
42-
return res
43-
48+
return copy
4449
}

‎lcof/面试题35. 复杂链表的复制/Solution.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,35 @@ public Node(int val) {
1212
}
1313
}
1414
*/
15+
1516
class Solution {
1617
public Node copyRandomList(Node head) {
1718
if (head == null) {
1819
return null;
1920
}
20-
Map<Node, Node> map = new HashMap<>();
21-
Node copyHead = new Node(-1);
22-
Node cur = copyHead, t = head;
23-
while (t != null) {
24-
Node node = new Node(t.val);
25-
map.put(t, node);
21+
22+
Node cur = head;
23+
while (cur != null) {
24+
Node node = new Node(cur.val);
25+
node.next = cur.next;
2626
cur.next = node;
27-
cur = node;
28-
t = t.next;
27+
cur = node.next;
2928
}
30-
cur = copyHead.next;
31-
while (head != null) {
32-
cur.random = map.get(head.random);
33-
cur = cur.next;
34-
head = head.next;
29+
30+
cur = head;
31+
while (cur != null) {
32+
cur.next.random = cur.random == null ? null : cur.random.next;
33+
cur = cur.next.next;
3534
}
36-
return copyHead.next;
3735

36+
Node copy = head.next;
37+
cur = head;
38+
while (cur != null) {
39+
Node next = cur.next;
40+
cur.next = next.next;
41+
next.next = next.next == null ? null : next.next.next;
42+
cur = cur.next;
43+
}
44+
return copy;
3845
}
3946
}

‎lcof/面试题35. 复杂链表的复制/Solution.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,35 @@
66
* this.random = random;
77
* };
88
*/
9+
910
/**
1011
* @param {Node} head
1112
* @return {Node}
1213
*/
13-
var copyRandomList = function (head) {
14-
function copy(node) {
15-
if (!node) return null;
16-
if (isRead.get(node)) return isRead.get(node);
17-
let newNode = new Node(node.val);
18-
isRead.set(node, newNode);
19-
newNode.random = copy(node.random);
20-
newNode.next = copy(node.next);
21-
return newNode;
14+
var copyRandomList = function(head) {
15+
if (head == null) {
16+
return null;
2217
}
23-
let isRead = new Map();
24-
return copy(head);
25-
};
18+
let cur = head;
19+
while (cur != null) {
20+
let node = new Node(cur.val, cur.next);
21+
cur.next = node;
22+
cur = node.next;
23+
}
24+
25+
cur = head;
26+
while (cur != null) {
27+
cur.next.random = cur.random == null ? null : cur.random.next;
28+
cur = cur.next.next;
29+
}
30+
31+
let copy = head.next;
32+
cur = head;
33+
while (cur != null) {
34+
let next = cur.next;
35+
cur.next = next.next;
36+
next.next = next.next == null ? null : next.next.next;
37+
cur = cur.next;
38+
}
39+
return copy;
40+
};

‎lcof/面试题35. 复杂链表的复制/Solution.py

+21-14
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
77
self.random = random
88
"""
99

10-
1110
class Solution:
1211
def copyRandomList(self, head: 'Node') -> 'Node':
13-
if not head:
12+
if head is None:
1413
return None
15-
copy_head = Node(-1)
16-
cur, t = copy_head, head
17-
cache = {}
18-
while t:
19-
cur.next = Node(t.val)
20-
cache[t] = cur.next
21-
cur, t = cur.next, t.next
22-
t, cur = head, copy_head.next
23-
while t:
24-
cur.random = cache.get(t.random)
25-
cur, t = cur.next, t.next
26-
return copy_head.next
14+
15+
cur = head
16+
while cur:
17+
node = Node(cur.val, cur.next)
18+
cur.next = node
19+
cur = node.next
20+
21+
cur = head
22+
while cur:
23+
cur.next.random = None if cur.random is None else cur.random.next
24+
cur = cur.next.next
25+
26+
copy = head.next
27+
cur = head
28+
while cur:
29+
next = cur.next
30+
cur.next = next.next
31+
next.next = None if next.next is None else next.next.next
32+
cur = cur.next
33+
return copy

‎solution/0100-0199/0138.Copy List with Random Pointer/README.md

+296-1
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,317 @@
7575

7676
<!-- 这里可写通用的实现逻辑 -->
7777

78+
首先,遍历链表,完成对每个旧节点的复制。
79+
80+
```bash
81+
A -> B -> C -> D -> null
82+
83+
=>
84+
85+
A -> A1 -> B -> B1 -> C -> C1 -> D -> D1 -> null
86+
```
87+
88+
接着设置新节点的 ramdom 指针。
89+
90+
然后遍历链表,修改旧节点和新节点的指向,将旧节点指向下一个旧节点,而新节点指向下一个新节点。
91+
92+
最后返回第一个新节点即可。
93+
7894
<!-- tabs:start -->
7995

8096
### **Python3**
8197

8298
<!-- 这里可写当前语言的特殊实现逻辑 -->
8399

84100
```python
85-
101+
"""
102+
# Definition for a Node.
103+
class Node:
104+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
105+
self.val = int(x)
106+
self.next = next
107+
self.random = random
108+
"""
109+
110+
class Solution:
111+
def copyRandomList(self, head: 'Node') -> 'Node':
112+
if head is None:
113+
return None
114+
115+
cur = head
116+
while cur:
117+
node = Node(cur.val, cur.next)
118+
cur.next = node
119+
cur = node.next
120+
121+
cur = head
122+
while cur:
123+
cur.next.random = None if cur.random is None else cur.random.next
124+
cur = cur.next.next
125+
126+
copy = head.next
127+
cur = head
128+
while cur:
129+
next = cur.next
130+
cur.next = next.next
131+
next.next = None if next.next is None else next.next.next
132+
cur = cur.next
133+
return copy
86134
```
87135

88136
### **Java**
89137

90138
<!-- 这里可写当前语言的特殊实现逻辑 -->
91139

92140
```java
141+
/*
142+
// Definition for a Node.
143+
class Node {
144+
int val;
145+
Node next;
146+
Node random;
147+
148+
public Node(int val) {
149+
this.val = val;
150+
this.next = null;
151+
this.random = null;
152+
}
153+
}
154+
*/
155+
156+
class Solution {
157+
public Node copyRandomList(Node head) {
158+
if (head == null) {
159+
return null;
160+
}
161+
162+
Node cur = head;
163+
while (cur != null) {
164+
Node node = new Node(cur.val);
165+
node.next = cur.next;
166+
cur.next = node;
167+
cur = node.next;
168+
}
169+
170+
cur = head;
171+
while (cur != null) {
172+
cur.next.random = cur.random == null ? null : cur.random.next;
173+
cur = cur.next.next;
174+
}
175+
176+
Node copy = head.next;
177+
cur = head;
178+
while (cur != null) {
179+
Node next = cur.next;
180+
cur.next = next.next;
181+
next.next = next.next == null ? null : next.next.next;
182+
cur = cur.next;
183+
}
184+
return copy;
185+
}
186+
}
187+
```
188+
189+
### **C++**
190+
191+
```cpp
192+
/*
193+
// Definition for a Node.
194+
class Node {
195+
public:
196+
int val;
197+
Node* next;
198+
Node* random;
199+
200+
Node(int _val) {
201+
val = _val;
202+
next = NULL;
203+
random = NULL;
204+
}
205+
};
206+
*/
207+
208+
class Solution {
209+
public:
210+
Node* copyRandomList(Node* head) {
211+
if (!head) {
212+
return nullptr;
213+
}
214+
Node* cur = head;
215+
while (cur) {
216+
Node* node = new Node(cur->val);
217+
node->next = cur->next;
218+
cur->next = node;
219+
cur = node->next;
220+
}
221+
222+
cur = head;
223+
while (cur) {
224+
cur->next->random = cur->random ? cur->random->next : nullptr;
225+
cur = cur->next->next;
226+
}
227+
228+
Node* copy = head->next;
229+
cur = head;
230+
while (cur) {
231+
Node* next = cur->next;
232+
cur->next = next->next;
233+
next->next = next->next ? next->next->next : nullptr;
234+
cur = cur->next;
235+
}
236+
return copy;
237+
}
238+
};
239+
```
240+
241+
### **C#**
242+
243+
```cs
244+
/*
245+
// Definition for a Node.
246+
public class Node {
247+
public int val;
248+
public Node next;
249+
public Node random;
250+
251+
public Node(int _val) {
252+
val = _val;
253+
next = null;
254+
random = null;
255+
}
256+
}
257+
*/
258+
259+
public class Solution {
260+
public Node CopyRandomList(Node head) {
261+
if (head == null) {
262+
return null;
263+
}
264+
265+
Node cur = head;
266+
while (cur != null) {
267+
Node node = new Node(cur.val);
268+
node.next = cur.next;
269+
cur.next = node;
270+
cur = node.next;
271+
}
272+
273+
cur = head;
274+
while (cur != null) {
275+
cur.next.random = cur.random == null ? null : cur.random.next;
276+
cur = cur.next.next;
277+
}
278+
279+
Node copy = head.next;
280+
cur = head;
281+
while (cur != null) {
282+
Node next = cur.next;
283+
cur.next = next.next;
284+
next.next = next.next == null ? null : next.next.next;
285+
cur = cur.next;
286+
}
287+
return copy;
288+
}
289+
}
290+
```
291+
292+
### **Go**
293+
294+
```go
295+
/**
296+
* Definition for a Node.
297+
* type Node struct {
298+
* Val int
299+
* Next *Node
300+
* Random *Node
301+
* }
302+
*/
303+
304+
func copyRandomList(head *Node) *Node {
305+
if head == nil {
306+
return nil
307+
}
308+
309+
cur := head
310+
for cur != nil {
311+
node := &Node{
312+
Val: cur.Val,
313+
Next: cur.Next,
314+
Random: nil,
315+
}
316+
cur.Next = node
317+
cur = node.Next
318+
}
319+
320+
cur = head
321+
for cur != nil {
322+
if cur.Random == nil {
323+
cur.Next.Random = nil
324+
} else {
325+
cur.Next.Random = cur.Random.Next
326+
}
327+
cur = cur.Next.Next
328+
}
329+
330+
copy := head.Next
331+
cur = head
332+
for cur != nil {
333+
next := cur.Next
334+
cur.Next = next.Next
335+
if (next.Next == nil) {
336+
next.Next = nil
337+
} else {
338+
next.Next = next.Next.Next
339+
}
340+
cur = cur.Next
341+
}
342+
return copy
343+
}
344+
```
93345

346+
### **JavaScript**
347+
348+
```js
349+
/**
350+
* // Definition for a Node.
351+
* function Node(val, next, random) {
352+
* this.val = val;
353+
* this.next = next;
354+
* this.random = random;
355+
* };
356+
*/
357+
358+
/**
359+
* @param {Node} head
360+
* @return {Node}
361+
*/
362+
var copyRandomList = function(head) {
363+
if (head == null) {
364+
return null;
365+
}
366+
let cur = head;
367+
while (cur != null) {
368+
let node = new Node(cur.val, cur.next);
369+
cur.next = node;
370+
cur = node.next;
371+
}
372+
373+
cur = head;
374+
while (cur != null) {
375+
cur.next.random = cur.random == null ? null : cur.random.next;
376+
cur = cur.next.next;
377+
}
378+
379+
let copy = head.next;
380+
cur = head;
381+
while (cur != null) {
382+
let next = cur.next;
383+
cur.next = next.next;
384+
next.next = next.next == null ? null : next.next.next;
385+
cur = cur.next;
386+
}
387+
return copy;
388+
};
94389
```
95390

96391
### **...**

‎solution/0100-0199/0138.Copy List with Random Pointer/README_EN.md

+279
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,292 @@
7070
### **Python3**
7171

7272
```python
73+
"""
74+
# Definition for a Node.
75+
class Node:
76+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
77+
self.val = int(x)
78+
self.next = next
79+
self.random = random
80+
"""
7381

82+
class Solution:
83+
def copyRandomList(self, head: 'Node') -> 'Node':
84+
if head is None:
85+
return None
86+
87+
cur = head
88+
while cur:
89+
node = Node(cur.val, cur.next)
90+
cur.next = node
91+
cur = node.next
92+
93+
cur = head
94+
while cur:
95+
cur.next.random = None if cur.random is None else cur.random.next
96+
cur = cur.next.next
97+
98+
copy = head.next
99+
cur = head
100+
while cur:
101+
next = cur.next
102+
cur.next = next.next
103+
next.next = None if next.next is None else next.next.next
104+
cur = cur.next
105+
return copy
74106
```
75107

76108
### **Java**
77109

78110
```java
111+
/*
112+
// Definition for a Node.
113+
class Node {
114+
int val;
115+
Node next;
116+
Node random;
117+
118+
public Node(int val) {
119+
this.val = val;
120+
this.next = null;
121+
this.random = null;
122+
}
123+
}
124+
*/
125+
126+
class Solution {
127+
public Node copyRandomList(Node head) {
128+
if (head == null) {
129+
return null;
130+
}
131+
132+
Node cur = head;
133+
while (cur != null) {
134+
Node node = new Node(cur.val);
135+
node.next = cur.next;
136+
cur.next = node;
137+
cur = node.next;
138+
}
139+
140+
cur = head;
141+
while (cur != null) {
142+
cur.next.random = cur.random == null ? null : cur.random.next;
143+
cur = cur.next.next;
144+
}
145+
146+
Node copy = head.next;
147+
cur = head;
148+
while (cur != null) {
149+
Node next = cur.next;
150+
cur.next = next.next;
151+
next.next = next.next == null ? null : next.next.next;
152+
cur = cur.next;
153+
}
154+
return copy;
155+
}
156+
}
157+
```
158+
159+
### **C++**
160+
161+
```cpp
162+
/*
163+
// Definition for a Node.
164+
class Node {
165+
public:
166+
int val;
167+
Node* next;
168+
Node* random;
169+
170+
Node(int _val) {
171+
val = _val;
172+
next = NULL;
173+
random = NULL;
174+
}
175+
};
176+
*/
177+
178+
class Solution {
179+
public:
180+
Node* copyRandomList(Node* head) {
181+
if (!head) {
182+
return nullptr;
183+
}
184+
Node* cur = head;
185+
while (cur) {
186+
Node* node = new Node(cur->val);
187+
node->next = cur->next;
188+
cur->next = node;
189+
cur = node->next;
190+
}
191+
192+
cur = head;
193+
while (cur) {
194+
cur->next->random = cur->random ? cur->random->next : nullptr;
195+
cur = cur->next->next;
196+
}
197+
198+
Node* copy = head->next;
199+
cur = head;
200+
while (cur) {
201+
Node* next = cur->next;
202+
cur->next = next->next;
203+
next->next = next->next ? next->next->next : nullptr;
204+
cur = cur->next;
205+
}
206+
return copy;
207+
}
208+
};
209+
```
210+
211+
### **C#**
212+
213+
```cs
214+
/*
215+
// Definition for a Node.
216+
public class Node {
217+
public int val;
218+
public Node next;
219+
public Node random;
220+
221+
public Node(int _val) {
222+
val = _val;
223+
next = null;
224+
random = null;
225+
}
226+
}
227+
*/
228+
229+
public class Solution {
230+
public Node CopyRandomList(Node head) {
231+
if (head == null) {
232+
return null;
233+
}
234+
235+
Node cur = head;
236+
while (cur != null) {
237+
Node node = new Node(cur.val);
238+
node.next = cur.next;
239+
cur.next = node;
240+
cur = node.next;
241+
}
242+
243+
cur = head;
244+
while (cur != null) {
245+
cur.next.random = cur.random == null ? null : cur.random.next;
246+
cur = cur.next.next;
247+
}
248+
249+
Node copy = head.next;
250+
cur = head;
251+
while (cur != null) {
252+
Node next = cur.next;
253+
cur.next = next.next;
254+
next.next = next.next == null ? null : next.next.next;
255+
cur = cur.next;
256+
}
257+
return copy;
258+
}
259+
}
260+
```
261+
262+
### **Go**
263+
264+
```go
265+
/**
266+
* Definition for a Node.
267+
* type Node struct {
268+
* Val int
269+
* Next *Node
270+
* Random *Node
271+
* }
272+
*/
273+
274+
func copyRandomList(head *Node) *Node {
275+
if head == nil {
276+
return nil
277+
}
278+
279+
cur := head
280+
for cur != nil {
281+
node := &Node{
282+
Val: cur.Val,
283+
Next: cur.Next,
284+
Random: nil,
285+
}
286+
cur.Next = node
287+
cur = node.Next
288+
}
289+
290+
cur = head
291+
for cur != nil {
292+
if cur.Random == nil {
293+
cur.Next.Random = nil
294+
} else {
295+
cur.Next.Random = cur.Random.Next
296+
}
297+
cur = cur.Next.Next
298+
}
299+
300+
copy := head.Next
301+
cur = head
302+
for cur != nil {
303+
next := cur.Next
304+
cur.Next = next.Next
305+
if (next.Next == nil) {
306+
next.Next = nil
307+
} else {
308+
next.Next = next.Next.Next
309+
}
310+
cur = cur.Next
311+
}
312+
return copy
313+
}
314+
```
315+
316+
### **JavaScript**
317+
318+
```js
319+
/**
320+
* // Definition for a Node.
321+
* function Node(val, next, random) {
322+
* this.val = val;
323+
* this.next = next;
324+
* this.random = random;
325+
* };
326+
*/
327+
328+
/**
329+
* @param {Node} head
330+
* @return {Node}
331+
*/
332+
var copyRandomList = function(head) {
333+
if (head == null) {
334+
return null;
335+
}
336+
let cur = head;
337+
while (cur != null) {
338+
let node = new Node(cur.val, cur.next);
339+
cur.next = node;
340+
cur = node.next;
341+
}
342+
343+
cur = head;
344+
while (cur != null) {
345+
cur.next.random = cur.random == null ? null : cur.random.next;
346+
cur = cur.next.next;
347+
}
79348

349+
let copy = head.next;
350+
cur = head;
351+
while (cur != null) {
352+
let next = cur.next;
353+
cur.next = next.next;
354+
next.next = next.next == null ? null : next.next.next;
355+
cur = cur.next;
356+
}
357+
return copy;
358+
};
80359
```
81360

82361
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* next;
7+
Node* random;
8+
9+
Node(int _val) {
10+
val = _val;
11+
next = NULL;
12+
random = NULL;
13+
}
14+
};
15+
*/
16+
17+
class Solution {
18+
public:
19+
Node* copyRandomList(Node* head) {
20+
if (!head) {
21+
return nullptr;
22+
}
23+
Node* cur = head;
24+
while (cur) {
25+
Node* node = new Node(cur->val);
26+
node->next = cur->next;
27+
cur->next = node;
28+
cur = node->next;
29+
}
30+
31+
cur = head;
32+
while (cur) {
33+
cur->next->random = cur->random ? cur->random->next : nullptr;
34+
cur = cur->next->next;
35+
}
36+
37+
Node* copy = head->next;
38+
cur = head;
39+
while (cur) {
40+
Node* next = cur->next;
41+
cur->next = next->next;
42+
next->next = next->next ? next->next->next : nullptr;
43+
cur = cur->next;
44+
}
45+
return copy;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
1-
using System;
2-
using System.Collections.Generic;
1+
/*
2+
// Definition for a Node.
3+
public class Node {
4+
public int val;
5+
public Node next;
6+
public Node random;
7+
8+
public Node(int _val) {
9+
val = _val;
10+
next = null;
11+
random = null;
12+
}
13+
}
14+
*/
315

416
public class Solution {
517
public Node CopyRandomList(Node head) {
618
if (head == null) {
719
return null;
820
}
9-
10-
var map = new Dictionary<Node, Node>();
11-
var current = head;
12-
while (current != null) {
13-
var newCurrent = new Node(current.val);
14-
map.Add(current, newCurrent);
15-
current = current.next;
21+
22+
Node cur = head;
23+
while (cur != null) {
24+
Node node = new Node(cur.val);
25+
node.next = cur.next;
26+
cur.next = node;
27+
cur = node.next;
1628
}
1729

18-
foreach (var entry in map) {
19-
var oldNode = entry.Key;
20-
var newNode = entry.Value;
21-
if (oldNode.next != null) {
22-
newNode.next = map[oldNode.next];
23-
}
24-
if (oldNode.random != null) {
25-
newNode.random = map[oldNode.random];
26-
}
30+
cur = head;
31+
while (cur != null) {
32+
cur.next.random = cur.random == null ? null : cur.random.next;
33+
cur = cur.next.next;
2734
}
2835

29-
return map[head];
36+
Node copy = head.next;
37+
cur = head;
38+
while (cur != null) {
39+
Node next = cur.next;
40+
cur.next = next.next;
41+
next.next = next.next == null ? null : next.next.next;
42+
cur = cur.next;
43+
}
44+
return copy;
3045
}
3146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a Node.
3+
* type Node struct {
4+
* Val int
5+
* Next *Node
6+
* Random *Node
7+
* }
8+
*/
9+
10+
func copyRandomList(head *Node) *Node {
11+
if head == nil {
12+
return nil
13+
}
14+
15+
cur := head
16+
for cur != nil {
17+
node := &Node{
18+
Val: cur.Val,
19+
Next: cur.Next,
20+
Random: nil,
21+
}
22+
cur.Next = node
23+
cur = node.Next
24+
}
25+
26+
cur = head
27+
for cur != nil {
28+
if cur.Random == nil {
29+
cur.Next.Random = nil
30+
} else {
31+
cur.Next.Random = cur.Random.Next
32+
}
33+
cur = cur.Next.Next
34+
}
35+
36+
copy := head.Next
37+
cur = head
38+
for cur != nil {
39+
next := cur.Next
40+
cur.Next = next.Next
41+
if (next.Next == nil) {
42+
next.Next = nil
43+
} else {
44+
next.Next = next.Next.Next
45+
}
46+
cur = cur.Next
47+
}
48+
return copy
49+
}
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
/**
2-
* Definition for singly-linked list with a random pointer.
3-
* class RandomListNode {
4-
* int label;
5-
* RandomListNode next, random;
6-
* RandomListNode(int x) { this.label = x; }
7-
* };
8-
*/
9-
public class Solution {
10-
public RandomListNode copyRandomList(RandomListNode head) {
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
int val;
5+
Node next;
6+
Node random;
7+
8+
public Node(int val) {
9+
this.val = val;
10+
this.next = null;
11+
this.random = null;
12+
}
13+
}
14+
*/
15+
16+
class Solution {
17+
public Node copyRandomList(Node head) {
1118
if (head == null) {
1219
return null;
1320
}
14-
15-
// step1
16-
RandomListNode cur = head;
21+
22+
Node cur = head;
1723
while (cur != null) {
18-
RandomListNode node = new RandomListNode(cur.label);
24+
Node node = new Node(cur.val);
1925
node.next = cur.next;
2026
cur.next = node;
2127
cur = node.next;
2228
}
23-
24-
// step2
29+
2530
cur = head;
2631
while (cur != null) {
27-
RandomListNode clone = cur.next;
28-
if (cur.random != null) {
29-
clone.random = cur.random.next;
30-
}
31-
cur = clone.next;
32+
cur.next.random = cur.random == null ? null : cur.random.next;
33+
cur = cur.next.next;
3234
}
33-
34-
// step3
35+
36+
Node copy = head.next;
3537
cur = head;
36-
RandomListNode cloneHead = head.next;
37-
while (cur.next != null) {
38-
RandomListNode clone = cur.next;
39-
cur.next = clone.next;
40-
cur = clone;
38+
while (cur != null) {
39+
Node next = cur.next;
40+
cur.next = next.next;
41+
next.next = next.next == null ? null : next.next.next;
42+
cur = cur.next;
4143
}
42-
43-
return cloneHead;
44+
return copy;
4445
}
4546
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, next, random) {
4+
* this.val = val;
5+
* this.next = next;
6+
* this.random = random;
7+
* };
8+
*/
9+
10+
/**
11+
* @param {Node} head
12+
* @return {Node}
13+
*/
14+
var copyRandomList = function(head) {
15+
if (head == null) {
16+
return null;
17+
}
18+
let cur = head;
19+
while (cur != null) {
20+
let node = new Node(cur.val, cur.next);
21+
cur.next = node;
22+
cur = node.next;
23+
}
24+
25+
cur = head;
26+
while (cur != null) {
27+
cur.next.random = cur.random == null ? null : cur.random.next;
28+
cur = cur.next.next;
29+
}
30+
31+
let copy = head.next;
32+
cur = head;
33+
while (cur != null) {
34+
let next = cur.next;
35+
cur.next = next.next;
36+
next.next = next.next == null ? null : next.next.next;
37+
cur = cur.next;
38+
}
39+
return copy;
40+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
5+
self.val = int(x)
6+
self.next = next
7+
self.random = random
8+
"""
9+
10+
class Solution:
11+
def copyRandomList(self, head: 'Node') -> 'Node':
12+
if head is None:
13+
return None
14+
15+
cur = head
16+
while cur:
17+
node = Node(cur.val, cur.next)
18+
cur.next = node
19+
cur = node.next
20+
21+
cur = head
22+
while cur:
23+
cur.next.random = None if cur.random is None else cur.random.next
24+
cur = cur.next.next
25+
26+
copy = head.next
27+
cur = head
28+
while cur:
29+
next = cur.next
30+
cur.next = next.next
31+
next.next = None if next.next is None else next.next.next
32+
cur = cur.next
33+
return copy

0 commit comments

Comments
 (0)
Please sign in to comment.