Skip to content

Commit db15933

Browse files
committed
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
```

0 commit comments

Comments
 (0)