Advertisement
Khadija_Assem

Untitled

Jan 16th, 2020
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. int val;
  5. Node next;
  6. Node random;
  7.  
  8. public Node(int val) {
  9. this.val = val;
  10. this.next = null;
  11. this.random = null;
  12. }
  13. }
  14. */
  15. class Solution {
  16. public Node copyRandomList(Node head) {
  17. HashMap<Node,Node> map = new HashMap<>();
  18. Node old = head;
  19. Node New = null;
  20. while(old!= null){
  21. New = new Node(old.val);
  22. map.put(old,New);
  23. old = old.next;
  24. }
  25. old = head;
  26. while (old!=null){
  27. New = map.get(old);
  28. New.next = map.get(old.next);
  29. New.random = map.get(old.random);
  30. old = old.next;
  31. }
  32. return map.get(head);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement