Skip to content

Commit

Permalink
Create 138-Copy-List-with-Random-Pointer.java
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaTushar1 authored Jun 24, 2022
1 parent afb5cc1 commit 3895a4f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/138-Copy-List-with-Random-Pointer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public Node copyRandomList(Node head) {
Node cur = head;
HashMap<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);
}
}

0 comments on commit 3895a4f

Please sign in to comment.