leetcode problem - 138. Copy List with Random Pointer

Significance: Copy a list in depth. The list contains not only a next pointer, but also a random pointer, which points to a node in the string or is empty.

Nodes are defined as:

// Definition for singly-linked list.
 public class RandomListNode {
     int label;
     RandomListNode next, random;
     RandomListNode(int x) { this.label = x; }
 }

Assuming that the original list is as follows, the thin line represents the next pointer, the thick line represents the random pointer, and the pointers that are not drawn all point to NULL:

There is a clever solution to the problem, just remember it. When building a new node, the pointer changes as follows: insert the new node behind the corresponding old node:

Two steps

1. Construct a new node random pointer: new1 - > Random = old1 - > Random - > next, new2-random = NULL, new3-random = NULL, new4 - > Random = old4 - > Random - > next

2. Restore the original list and build a new list: for example, old1 - > next = old1 - > next - > next - > next, new1 - > next = new1 - > next - > next

Time complexity O(N) and space complexity O(1) of the algorithm

/**
 * Definition for singly-linked list with a random pointer.
 * class RandomListNode {
 *     int label;
 *     RandomListNode next, random;
 *     RandomListNode(int x) { this.label = x; }
 * };
 */
class Solution {

    public static RandomListNode copyRandomList(RandomListNode head) {
         if(head == null) return null;
         RandomListNode fastNode = head;
         while(fastNode != null){
             RandomListNode insertNode = new RandomListNode(fastNode.label);
             insertNode.next = fastNode.next;
             fastNode.next = insertNode;
             fastNode = fastNode.next.next;
         }
         fastNode = head;
         while(fastNode != null){
             if(fastNode.random != null){
                 fastNode.next.random = fastNode.random.next;
             }
             fastNode = fastNode.next.next;
         }
         fastNode = head;
         RandomListNode newHead = head.next;
         while(fastNode != null){
             RandomListNode newNode = fastNode.next;
             fastNode.next = newNode.next;
             if(newNode.next != null){
                 newNode.next = newNode.next.next;
             }
             fastNode = fastNode.next;
         }

         return newHead;
    }
    public static void print(RandomListNode head){
        while(head != null){
            System.out.println(head.label);
            head = head.next;
        }

    }
    public static void main(String[] args) {
        RandomListNode l1 = new RandomListNode(1);
        RandomListNode l2 = new RandomListNode(2);
        RandomListNode l3 = new RandomListNode(3);
        RandomListNode l4 = new RandomListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l1.random = l3;
        l4.random = l2;
        print(copyRandomList(l1));
    }
}

Posted by saunders1989 on Wed, 06 Feb 2019 19:51:16 -0800