138. Copy List with Random Pointer

Posted 鸵鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了138. Copy List with Random Pointer相关的知识,希望对你有一定的参考价值。

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        Map<RandomListNode, RandomListNode> map=new HashMap<RandomListNode,RandomListNode>();
        RandomListNode p=new RandomListNode(0);
        p.next=head;
        RandomListNode res=new RandomListNode(0);
        RandomListNode q=res;
        while(p!=null)
        {
            if(p.next!=null)
            {
                if(!map.containsKey(p.next))
                    map.put(p.next, new RandomListNode(p.next.label));
                q.next=map.get(p.next);
            }
            if(p.random!=null)
            {
                if(!map.containsKey(p.random))
                    map.put(p.random,new RandomListNode(p.random.label));
                q.random=map.get(p.random);
            }
            p=p.next;
            q=q.next;
        }
        return res.next;
    }
}

  

以上是关于138. Copy List with Random Pointer的主要内容,如果未能解决你的问题,请参考以下文章

138. Copy List with Random Pointer

138. Copy List with Random Pointer

138 Copy List with Random Pointer

138. Copy List with Random Pointer

138. Copy List with Random Pointer(js)

138. Copy List with Random Pointer