Skip to content

Commit 274711e

Browse files
Create 138-Copy-List-with-Random-Pointer.java
1 parent 46ff512 commit 274711e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public Node copyRandomList(Node head) {
3+
Node cur = head;
4+
HashMap<Node, Node> map = new HashMap<>();
5+
while (cur!=null) {
6+
map.put(cur, new Node(cur.val));
7+
cur = cur.next;
8+
}
9+
cur = head;
10+
while (cur!=null) {
11+
map.get(cur).next = map.get(cur.next);
12+
map.get(cur).random = map.get(cur.random);
13+
cur = cur.next;
14+
}
15+
return map.get(head);
16+
}
17+
}

0 commit comments

Comments
 (0)