Copy List with Random Pointer

題目

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.

思路
Given List A
First Pass
List B = Clone List A based next pointer
When cloning B, build the following two maps
First map, maps node in A to corresponding nodes in B
Second map, maps source and target of some random pointer in A

Second Pass
Iterate through the list B
Let's say current node is called curr
To add random pointer for curr, add edge (first_map.get(curr), first_map.get(second_map.get(curr)))

答案

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if(head == null) return null;
        HashMap<RandomListNode, RandomListNode> clone_map = new HashMap<RandomListNode, RandomListNode>();
        HashMap<RandomListNode, RandomListNode> ranptr_map = new HashMap<RandomListNode, RandomListNode>();
        
        // First pass, clone list
        RandomListNode cloned = null, list1 = head, list2 = null;
        while(list1 != null) {
            if(cloned == null) {
                cloned = new RandomListNode(list1.label);
                list2 = cloned;
            }
            else {
                list2.next = new RandomListNode(list1.label);
                list2 = list2.next;
            }
            clone_map.put(list1, list2);
            ranptr_map.put(list1, list1.random);
            list1 = list1.next;
        }
        // Second pass, add random pointers
        list1 = head;
        list2 = cloned;
        while(list1 != null) {
            list2.random = clone_map.get(ranptr_map.get(list1));
            list2 = list2.next;
            list1 = list1.next;
        }
        return cloned;
    }
}

另一種簡單的實現(xiàn)思路(在面試中用這種方法更容易實現(xiàn))

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if (head == null) {
            return null;
        }
        
        final Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();

        RandomListNode cur = head;
        while(cur != null) {
            map.put(cur, new RandomListNode(cur.label));
            cur = cur.next;
        }
        
        for (Map.Entry<RandomListNode, RandomListNode> entry : map.entrySet()) {
            final RandomListNode newNode = entry.getValue();
            newNode.next = map.get(entry.getKey().next);
            newNode.random = map.get(entry.getKey().random);
        }
        
        return map.get(head);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容