/
_0138_CopyListWithRandomPointer.java
192 lines (177 loc) · 5.99 KB
/
_0138_CopyListWithRandomPointer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.diguage.algorithm.leetcode;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* = 138. Copy List with Random Pointer
*
* https://leetcode.com/problems/copy-list-with-random-pointer/[Copy List with Random Pointer - LeetCode]
*
* A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
*
* Return a deep copy of the list.
*
* The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
*
* * `val`: an integer representing `Node.val`
* * `random_index`: the index of the node (range from `0` to `n-1`) where random pointer points to, or `null` if it does not point to any node.
*
* .Example 1:
* [source]
* ----
* Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
* Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
* ----
*
* .Example 2:
* [source]
* ----
* Input: head = [[1,1],[2,1]]
* Output: [[1,1],[2,1]]
* ----
*
* .Example 3:
* [source]
* ----
* Input: head = [[3,null],[3,0],[3,null]]
* Output: [[3,null],[3,0],[3,null]]
* ----
*
* .Example 4:
* [source]
* ----
* Input: head = []
* Output: []
* Explanation: Given linked list is empty (null pointer), so return null.
* ----
*
* *Constraints:*
*
* * `-10000 <= Node.val <= 10000`
* * `Node.random` is null or pointing to a node in the linked list.
* * Number of Nodes will not exceed 1000.
*
* @author D瓜哥, https://www.diguage.com/
* @since 2020-01-07 22:21
*/
public class _0138_CopyListWithRandomPointer {
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Copy List with Random Pointer.
*
* Memory Usage: 40.6 MB, less than 5.61% of Java online submissions for Copy List with Random Pointer.
*
* Copy from: https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43488/Java-O(n)-solution[Java O(n) solution - LeetCode Discuss]
*/
public Node copyRandomList(Node head) {
if (Objects.isNull(head)) {
return null;
}
Map<Node, Node> dadToChildMap = new HashMap<>();
Node current = head;
while (Objects.nonNull(current)) {
dadToChildMap.put(current, new Node(current.val));
current = current.next;
}
current = head;
while (Objects.nonNull(current)) {
dadToChildMap.get(current).next = dadToChildMap.get(current.next);
dadToChildMap.get(current).random = dadToChildMap.get(current.random);
current = current.next;
}
return dadToChildMap.get(head);
}
/**
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Copy List with Random Pointer.
*
* Memory Usage: 39.2 MB, less than 5.61% of Java online submissions for Copy List with Random Pointer.
*
* Copy form: https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43491/A-solution-with-constant-space-complexity-O(1)-and-linear-time-complexity-O(N)[A solution with constant space complexity O(1) and linear time complexity O(N) - LeetCode Discuss]
*/
public Node copyRandomListLink(Node head) {
if (Objects.isNull(head)) {
return null;
}
// 第一步:把原始链表和结果链表混编起来。
Node acNode = head;
while (Objects.nonNull(acNode)) {
Node node = new Node(acNode.val);
node.next = acNode.next;
acNode.next = node;
acNode = node.next;
}
// 第二步:复制 random 指针
acNode = head;
while (Objects.nonNull(acNode)) {
Node random = acNode.random;
if (Objects.nonNull(random)) {
Node next = acNode.next;
next.random = random.next;
}
acNode = acNode.next.next;
}
// 第三步:拆分出结果链表
// Node result = head.next;
// Node rcNode = result;
// acNode = head;
// while (Objects.nonNull(rcNode)) {
// if (Objects.nonNull(rcNode.next)) {
// acNode.next = rcNode.next;
// rcNode.next = rcNode.next.next;
// acNode = acNode.next;
// }
// rcNode = rcNode.next;
// }
// acNode.next = null;
Node prefixHead = new Node(0);
Node rcNode = prefixHead;
acNode = head;
while (Objects.nonNull(acNode)) {
rcNode.next = acNode.next;
acNode.next = acNode.next.next;
acNode = acNode.next;
rcNode = rcNode.next;
}
return prefixHead.next;
}
public static void main(String[] args) {
_0138_CopyListWithRandomPointer solution = new _0138_CopyListWithRandomPointer();
Integer[][] a1 = {{7, null}, {13, 0}, {11, 4}, {10, 2}, {1, 0}};
Node l1 = build(a1);
Node r1 = solution.copyRandomList(l1);
Integer[][] a2 = {{1, 1}, {2, 1}};
Node l2 = build(a2);
Node r2 = solution.copyRandomList(l2);
}
public static Node build(Integer[][] array) {
Node restult = new Node(array[0][0]);
Node current = restult;
for (int i = 1; i < array.length; i++) {
Node next = new Node(array[i][0]);
current.next = next;
current = next;
}
current = restult;
for (int i = 0; i < array.length; i++) {
Integer[] ints = array[i];
if (Objects.nonNull(ints[1])) {
Node random = restult;
for (int j = 0; j < ints[1]; j++) {
random = random.next;
}
current.random = random;
}
current = current.next;
}
return restult;
}
static class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
}