-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0138.java
More file actions
76 lines (66 loc) · 1.99 KB
/
Copy pathLeetCode0138.java
File metadata and controls
76 lines (66 loc) · 1.99 KB
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
/* Copy List with Random Pointer
* Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
* Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
* */
import java.util.HashMap;
import java.util.Map;
public class LeetCode0138 {
public static void main(String args[]) {
Node node0 = new Node(7);
Node node1 = new Node(13);
Node node2 = new Node(11);
Node node3 = new Node(10);
Node node4 = new Node(1);
node0.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node4;
node1.random = node0;
node2.random = node4;
node3.random = node2;
node4.random = node0;
Node copy = copyRandomList(node0);
while (copy != null) {
System.out.print(copy.val + ",");
if (copy.random != null)
System.out.println(copy.random.val);
else
System.out.println("null");
copy = copy.next;
}
}
public static Node copyRandomList(Node head) {
if (head == null)
return null;
// 映射关系为 原节点->复制节点
Map<Node, Node> map = new HashMap<>();
// 将原节点进行复制(仅值,不包括指针),并存储到哈希表中
Node p = head;
while (p != null){
Node copy = new Node(p.val);
map.put(p, copy);
p = p.next;
}
// 修改复制节点的指针
p = head;
while (p != null){
Node copy = map.get(p);
if (p.next != null)
copy.next = map.get(p.next);
if (p.random != null)
copy.random = map.get(p.random);
p = p.next;
}
return map.get(head);
}
}
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}