Skip to content

Commit f8f4554

Browse files
committedApr 3, 2020
feat: add python and java solutions to lcof question
添加《剑指 Offer》题解:面试题35. 复杂链表的复制
1 parent 4a7883c commit f8f4554

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed
 

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

+63-1
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,76 @@
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```python
57+
"""
58+
# Definition for a Node.
59+
class Node:
60+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
61+
self.val = int(x)
62+
self.next = next
63+
self.random = random
64+
"""
65+
class Solution:
66+
def copyRandomList(self, head: 'Node') -> 'Node':
67+
if not head:
68+
return None
69+
copy_head = Node(-1)
70+
cur, t = copy_head, head
71+
cache = {}
72+
while t:
73+
cur.next = Node(t.val)
74+
cache[t] = cur.next
75+
cur, t = cur.next, t.next
76+
t, cur = head, copy_head.next
77+
while t:
78+
cur.random = cache.get(t.random)
79+
cur, t = cur.next, t.next
80+
return copy_head.next
5781

5882
```
5983

6084
### Java
6185
<!-- 这里可写当前语言的特殊实现逻辑 -->
6286

6387
```java
64-
88+
/*
89+
// Definition for a Node.
90+
class Node {
91+
int val;
92+
Node next;
93+
Node random;
94+
95+
public Node(int val) {
96+
this.val = val;
97+
this.next = null;
98+
this.random = null;
99+
}
100+
}
101+
*/
102+
class Solution {
103+
public Node copyRandomList(Node head) {
104+
if (head == null) {
105+
return null;
106+
}
107+
Map<Node, Node> map = new HashMap<>();
108+
Node copyHead = new Node(-1);
109+
Node cur = copyHead, t = head;
110+
while (t != null) {
111+
Node node = new Node(t.val);
112+
map.put(t, node);
113+
cur.next = node;
114+
cur = node;
115+
t = t.next;
116+
}
117+
cur = copyHead.next;
118+
while (head != null) {
119+
cur.random = map.get(head.random);
120+
cur = cur.next;
121+
head = head.next;
122+
}
123+
return copyHead.next;
124+
125+
}
126+
}
65127
```
66128

67129
### ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
class Solution {
16+
public Node copyRandomList(Node head) {
17+
if (head == null) {
18+
return null;
19+
}
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);
26+
cur.next = node;
27+
cur = node;
28+
t = t.next;
29+
}
30+
cur = copyHead.next;
31+
while (head != null) {
32+
cur.random = map.get(head.random);
33+
cur = cur.next;
34+
head = head.next;
35+
}
36+
return copyHead.next;
37+
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
11+
class Solution:
12+
def copyRandomList(self, head: 'Node') -> 'Node':
13+
if not head:
14+
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

0 commit comments

Comments
 (0)
Please sign in to comment.