-
Notifications
You must be signed in to change notification settings - Fork 0
/
138.copy-list-with-random-pointer.java
44 lines (40 loc) · 1.11 KB
/
138.copy-list-with-random-pointer.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
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
HashMap<Node, Node> hsm = new HashMap<Node, Node>();
if(head == null)
return null;
Node res = new Node(-10001);
Node ans = res;
while(head != null){
if(!hsm.containsKey(head))
hsm.put(head, new Node(head.val));
Node clonednod = hsm.get(head);
if(head.random != null){
if(!hsm.containsKey(head.random))
hsm.put(head.random, new Node(head.random.val));
clonednod.random = hsm.get(head.random);
}
res.next = clonednod;
res = res.next;
head = head.next;
}
// Node t = new Node(-1);
// hsm.put(t, t);
// if(hsm.containsKey(new Node(-1)))
// System.out.println(4);
return ans.next;
}
}