Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- // 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> map = new HashMap<>();
- Node old = head;
- Node New = null;
- while(old!= null){
- New = new Node(old.val);
- map.put(old,New);
- old = old.next;
- }
- old = head;
- while (old!=null){
- New = map.get(old);
- New.next = map.get(old.next);
- New.random = map.get(old.random);
- old = old.next;
- }
- return map.get(head);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement