Leetcode 138. Copy List with Random Pointer

O(n) time and O(1) space.

Three steps.

Copy all new nodes and link them next to the old one;

Copy all random links for new nodes;

Restore both old list and new list.

Example.

1->2->3->null

1->1`->2->2`->3->3`->null

Restore to,

1->2->3->null

1`->2`->3`->null

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if (head == null)
            return null;
        
        // Link all new nodes next to the old nodes
        RandomListNode temp = head;
        while (temp != null) {
            RandomListNode newNode = new RandomListNode(temp.label);
            RandomListNode next = temp.next;
            temp.next = newNode;
            newNode.next = next;
            temp = temp.next.next;
        }
        
        // Copy random links for new nodes
        temp = head;
        while (temp != null) {
            RandomListNode newNode = temp.next;
            if (temp.random != null)            // if random link in the origin list is null, iterate to the next
                newNode.random = temp.random.next;
            temp = temp.next.next;
        }
        
        // Set all next links in new nodes to new nodes
        RandomListNode newHead = head.next;     // save the head of new list, we will modify the next link of head (origin list)
        temp = head;
        while (temp != null) {
            RandomListNode next = temp.next.next;
            RandomListNode newNode = temp.next;
            if (next != null)           // take care of the end of the list
                newNode.next = next.next;
            temp.next = next;           // need to restore the origin list
            temp = next;
        }
        
        return newHead;
    }
}

Using a hashmap to save the <old, new> pairs,

then set next and random link for new created nodes.

O(n) time and space complexity.

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        // a hashmap to save <oldNode, newNode> pairs
        HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
        
        // create mapping between new nodes and old nodes
        RandomListNode temp = head;
        while (temp != null) {
            map.put(temp, new RandomListNode(temp.label));
            temp = temp.next;
        }
        
        // link next and random links for new nodes
        // iterate the hashmap 
        for (Map.Entry<RandomListNode, RandomListNode> entry : map.entrySet()) {
            // link next
            entry.getValue().next = map.get(entry.getKey().next);
            // link random
            entry.getValue().random = map.get(entry.getKey().random);
        }
        
        return map.get(head);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值