Advertisement
sweet1cris

Untitled

Sep 25th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. public class DeepCopyLinkedListRandom {
  2.   // Method 1: using HashMap to avoid copy multiple times for the same node.
  3.   public RandomListNode copy(RandomListNode head) {
  4.     if (head == null) {
  5.       return null;
  6.     }
  7.     // Sentinel node to help construct the deep copy.
  8.     RandomListNode dummy = new RandomListNode(0);
  9.     RandomListNode cur = dummy;
  10.     // Maintains the mapping between the node in the original list and
  11.     // the corresponding node in the new list.
  12.     Map<RandomListNode, RandomListNode> map = new HashMap<>();
  13.     while (head != null) {
  14.       // Copy the current node if necessary.
  15.       if (!map.containsKey(head)) {
  16.         map.put(head, new RandomListNode(head.value));
  17.       }
  18.       // Connect the copied node to the deep copy list.
  19.       cur.next = map.get(head);
  20.       // Copy the random node if necessary.
  21.       if (head.random != null) {
  22.         if (!map.containsKey(head.random)) {
  23.           map.put(head.random, new RandomListNode(head.random.value));
  24.         }
  25.         // Connect the copied node to the random pointer.
  26.         cur.next.random = map.get(head.random);
  27.       }
  28.       head = head.next;
  29.       cur = cur.next;
  30.     }
  31.     return dummy.next;
  32.   }
  33.  
  34.   // Method 2: Another three pass solution, not using HashMap,
  35.   // but changing the original list structure during the copy
  36.   // (it will be changed back at the end).
  37.   public RandomListNode copyII(RandomListNode head) {
  38.     if (head == null) {
  39.       return null;
  40.     }
  41.     // First pass, for each node in the original list, insert a
  42.     // copied node between the node and node.next.
  43.     RandomListNode cur = head;
  44.     while (cur != null) {
  45.  
  46.  
  47.       // Make a copy of cur node, insert it to the middle of cur and cur.next.
  48.       RandomListNode copy = new RandomListNode(cur.value);
  49.       copy.next = cur.next;
  50.       cur.next = copy;
  51.       cur = cur.next.next;
  52.     }
  53.     // Second pass, link the random pointer for the copied node.
  54.     cur = head;
  55.     while (cur != null) {
  56.       if (cur.random != null) {
  57.         cur.next.random = cur.random.next;
  58.       }
  59.       cur = cur.next.next;
  60.     }
  61.     // Third pass, extract the copied node.
  62.     cur = head;
  63.     RandomListNode dummy = new RandomListNode(0);
  64.     RandomListNode copyPrev = dummy;
  65.     while (cur != null) {
  66.       copyPrev.next = cur.next;
  67.       cur.next = cur.next.next;
  68.       copyPrev = copyPrev.next;
  69.       cur = cur.next;
  70.     }
  71.     return dummy.next;
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement