-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain35.java
More file actions
37 lines (33 loc) · 780 Bytes
/
Main35.java
File metadata and controls
37 lines (33 loc) · 780 Bytes
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
package JZOffer2;
import java.util.HashMap;
import java.util.Map;
public class Main35 {
public Node copyRandomList(Node head) {
if(head == null){
return null;
}
Node cur = head;
Map<Node, Node> map = new HashMap<>();
while(cur != null){
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
while(cur != null){
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.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;
}
}