Skip to content

Commit 11ef679

Browse files
committed
feat: add solutions to lcof problem: No.35
1 parent 7dbbefa commit 11ef679

File tree

15 files changed

+1064
-694
lines changed

15 files changed

+1064
-694
lines changed

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

+287-179
Large diffs are not rendered by default.

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

+14-18
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,39 @@ class Node {
55
int val;
66
Node* next;
77
Node* random;
8-
8+
99
Node(int _val) {
1010
val = _val;
1111
next = NULL;
1212
random = NULL;
1313
}
1414
};
1515
*/
16-
1716
class Solution {
1817
public:
1918
Node* copyRandomList(Node* head) {
2019
if (!head) {
2120
return nullptr;
2221
}
23-
Node* cur = head;
24-
while (cur) {
22+
for (Node* cur = head; cur; ) {
2523
Node* node = new Node(cur->val);
2624
node->next = cur->next;
2725
cur->next = node;
2826
cur = node->next;
2927
}
30-
31-
cur = head;
32-
while (cur) {
33-
cur->next->random = cur->random ? cur->random->next : nullptr;
34-
cur = cur->next->next;
28+
for (Node* cur = head; cur; cur = cur->next->next) {
29+
if (cur->random) {
30+
cur->next->random = cur->random->next;
31+
}
3532
}
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;
33+
Node* ans = head->next;
34+
for (Node* cur = head; cur; ) {
35+
Node* nxt = cur->next;
36+
if (nxt) {
37+
cur->next = nxt->next;
38+
}
39+
cur = nxt;
4440
}
45-
return copy;
41+
return ans;
4642
}
4743
};

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

+15-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class Node {
44
public int val;
55
public Node next;
66
public Node random;
7-
7+
88
public Node(int _val) {
99
val = _val;
1010
next = null;
@@ -18,29 +18,24 @@ public Node CopyRandomList(Node head) {
1818
if (head == null) {
1919
return null;
2020
}
21-
22-
Node cur = head;
23-
while (cur != null) {
24-
Node node = new Node(cur.val);
25-
node.next = cur.next;
21+
for (Node cur = head; cur != null; ) {
22+
Node node = new Node(cur.val, cur.next);
2623
cur.next = node;
2724
cur = node.next;
2825
}
29-
30-
cur = head;
31-
while (cur != null) {
32-
cur.next.random = cur.random == null ? null : cur.random.next;
33-
cur = cur.next.next;
26+
for (Node cur = head; cur != null; cur = cur.next.next) {
27+
if (cur.random != null) {
28+
cur.next.random = cur.random.next;
29+
}
3430
}
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;
31+
Node ans = head.next;
32+
for (Node cur = head; cur != null; ) {
33+
Node nxt = cur.next;
34+
if (nxt != null) {
35+
cur.next = nxt.next;
36+
}
37+
cur = nxt;
4338
}
44-
return copy;
39+
return ans;
4540
}
4641
}

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

+23-39
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,27 @@
77
* }
88
*/
99

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
10+
func copyRandomList(head *Node) *Node {
11+
if head == nil {
12+
return nil
13+
}
14+
for cur := head; cur != nil; {
15+
node := &Node{cur.Val, cur.Next, nil}
16+
cur.Next = node
17+
cur = node.Next
18+
}
19+
for cur := head; cur != nil; cur = cur.Next.Next {
20+
if cur.Random != nil {
21+
cur.Next.Random = cur.Random.Next
22+
}
23+
}
24+
ans := head.Next
25+
for cur := head; cur != nil; {
26+
nxt := cur.Next
27+
if nxt != nil {
28+
cur.Next = nxt.Next
29+
}
30+
cur = nxt
31+
}
32+
return ans
4933
}

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

+14-20
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,29 @@ public Node(int val) {
1212
}
1313
}
1414
*/
15-
1615
class Solution {
1716
public Node copyRandomList(Node head) {
1817
if (head == null) {
1918
return null;
2019
}
21-
22-
Node cur = head;
23-
while (cur != null) {
24-
Node node = new Node(cur.val);
25-
node.next = cur.next;
20+
for (Node cur = head; cur != null; ) {
21+
Node node = new Node(cur.val, cur.next);
2622
cur.next = node;
2723
cur = node.next;
2824
}
29-
30-
cur = head;
31-
while (cur != null) {
32-
cur.next.random = cur.random == null ? null : cur.random.next;
33-
cur = cur.next.next;
25+
for (Node cur = head; cur != null; cur = cur.next.next) {
26+
if (cur.random != null) {
27+
cur.next.random = cur.random.next;
28+
}
3429
}
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;
30+
Node ans = head.next;
31+
for (Node cur = head; cur != null; ) {
32+
Node nxt = cur.next;
33+
if (nxt != null) {
34+
cur.next = nxt.next;
35+
}
36+
cur = nxt;
4337
}
44-
return copy;
38+
return ans;
4539
}
4640
}

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

+15-18
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,26 @@
1212
* @return {Node}
1313
*/
1414
var copyRandomList = function (head) {
15-
if (head == null) {
15+
if (!head) {
1616
return null;
1717
}
18-
let cur = head;
19-
while (cur != null) {
20-
let node = new Node(cur.val, cur.next);
18+
for (let cur = head; cur; ) {
19+
const node = new Node(cur.val, cur.next, null);
2120
cur.next = node;
2221
cur = node.next;
2322
}
24-
25-
cur = head;
26-
while (cur != null) {
27-
cur.next.random = cur.random == null ? null : cur.random.next;
28-
cur = cur.next.next;
23+
for (let cur = head; cur; cur = cur.next.next) {
24+
if (cur.random) {
25+
cur.next.random = cur.random.next;
26+
}
2927
}
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;
28+
const ans = head.next;
29+
for (let cur = head; cur; ) {
30+
const nxt = cur.next;
31+
if (nxt) {
32+
cur.next = nxt.next;
33+
}
34+
cur = nxt;
3835
}
39-
return copy;
36+
return ans;
4037
};

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
99

1010

1111
class Solution:
12-
def copyRandomList(self, head: 'Node') -> 'Node':
12+
def copyRandomList(self, head: "Node") -> "Node":
1313
if head is None:
1414
return None
15-
1615
cur = head
1716
while cur:
1817
node = Node(cur.val, cur.next)
@@ -21,14 +20,15 @@ def copyRandomList(self, head: 'Node') -> 'Node':
2120

2221
cur = head
2322
while cur:
24-
cur.next.random = None if cur.random is None else cur.random.next
23+
if cur.random:
24+
cur.next.random = cur.random.next
2525
cur = cur.next.next
2626

27-
copy = head.next
27+
ans = head.next
2828
cur = head
2929
while cur:
30-
next = cur.next
31-
cur.next = next.next
32-
next.next = None if next.next is None else next.next.next
33-
cur = cur.next
34-
return copy
30+
nxt = cur.next
31+
if nxt:
32+
cur.next = nxt.next
33+
cur = nxt
34+
return ans

0 commit comments

Comments
 (0)