Skip to content

Commit 035fc4f

Browse files
committed
feat(solution): add solution 0138 [Java]
Copy list with random pointer
1 parent 584bcb0 commit 035fc4f

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
8989
| 0127 | [Word Ladder](https://github.com/doocs/leetcode/tree/master/solution/0127.Word%20Ladder) | `Breadth-first Search` |
9090
| 0130 | [Surrounded Regions](https://github.com/doocs/leetcode/tree/master/solution/0130.Surrounded%20Regions) | `Depth-first Search`, `Breadth-first Search`, `Union Find` |
9191
| 0137 | [Single Number II](https://github.com/doocs/leetcode/tree/master/solution/0137.Single%20Number%20II) | `Bit Manipulation` |
92+
| 0138 | [Copy List with Random Pointer](https://github.com/doocs/leetcode/tree/master/solution/0138.Copy%20List%20with%20Random%20Pointer) | `Hash Table`, `Linked List` |
9293
| 0142 | [Linked List Cycle II](https://github.com/doocs/leetcode/tree/master/solution/0142.Linked%20List%20Cycle%20II) | `Linked List`, `Two Pointers` |
9394
| 0143| [Reorder List](https://github.com/doocs/leetcode/tree/master/solution/0143.Reorder%20List) | `Linked List` |
9495
| 0144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/0144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |

img/random-list-step1.png

5 KB
Loading

img/random-list-step2.png

6 KB
Loading

img/random-list-step3.png

6 KB
Loading

img/random-list.png

3 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 复制带随机指针的链表
2+
3+
### 题目描述
4+
5+
给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
6+
7+
![random-list.png](/img/random-list.png)
8+
9+
要求返回这个链表的深度拷贝。
10+
11+
### 解法
12+
- 第一步,在每个节点的后面插入复制的节点;
13+
![random-list-step1.png](/img/random-list-step1.png)
14+
15+
- 第二步,对复制节点的 random 链接进行赋值;
16+
![random-list-step2.png](/img/random-list-step2.png)
17+
18+
- 第三步,分离两个链表。
19+
![random-list-step3.png](/img/random-list-step3.png)
20+
21+
```java
22+
/**
23+
* Definition for singly-linked list with a random pointer.
24+
* class RandomListNode {
25+
* int label;
26+
* RandomListNode next, random;
27+
* RandomListNode(int x) { this.label = x; }
28+
* };
29+
*/
30+
public class Solution {
31+
public RandomListNode copyRandomList(RandomListNode head) {
32+
if (head == null) {
33+
return null;
34+
}
35+
36+
// step1
37+
RandomListNode cur = head;
38+
while (cur != null) {
39+
RandomListNode node = new RandomListNode(cur.label);
40+
node.next = cur.next;
41+
cur.next = node;
42+
cur = node.next;
43+
}
44+
45+
// step2
46+
cur = head;
47+
while (cur != null) {
48+
RandomListNode clone = cur.next;
49+
if (cur.random != null) {
50+
clone.random = cur.random.next;
51+
}
52+
cur = clone.next;
53+
}
54+
55+
// step3
56+
cur = head;
57+
RandomListNode cloneHead = head.next;
58+
while (cur.next != null) {
59+
RandomListNode clone = cur.next;
60+
cur.next = clone.next;
61+
cur = clone;
62+
}
63+
64+
return cloneHead;
65+
}
66+
}
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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) {
11+
if (head == null) {
12+
return null;
13+
}
14+
15+
// step1
16+
RandomListNode cur = head;
17+
while (cur != null) {
18+
RandomListNode node = new RandomListNode(cur.label);
19+
node.next = cur.next;
20+
cur.next = node;
21+
cur = node.next;
22+
}
23+
24+
// step2
25+
cur = head;
26+
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+
}
33+
34+
// step3
35+
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;
41+
}
42+
43+
return cloneHead;
44+
}
45+
}

0 commit comments

Comments
 (0)