Copy List with Random Pointer

Copy List with Random Pointer

  Total Accepted: 5185  Total Submissions: 25214 My Submissions

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.


原理用一个hashmap<original node, cloned node> 来保存 原始node 和 克隆node 的映射。

第一种方法可以设置一个dummy head, 然后遍历原始的list, 只需要克隆原始node和 random pointer就可以了,next由dummy head 这个list去手动连接。

第二种是所有的东西一起克隆 (原始node, random pointer, next pointer), 这里就不需要dummy head了。直接克隆一遍就可以了。


    // method 1: only clone node and random pointer, let a dummy head to connect node's next.
    public static RandomListNode copyRandomList(RandomListNode head){
        if(head == null) return null;
        Map<RandomListNode,RandomListNode> map = new HashMap<>();
        RandomListNode dummyHead = new RandomListNode(0);
        RandomListNode temp = dummyHead;
        while(head!=null){
            // copy node
            RandomListNode cloned = null;
            if(map.containsKey(head)){
                cloned = map.get(head);
            }else{
                cloned = new RandomListNode(head.label);
                map.put(head,cloned);
            }
            temp.next = cloned;
            // copy node's random
            if(map.containsKey(head.random)){
                cloned.random = map.get(head.random);
            }else if(head.random!=null){
                RandomListNode random = new RandomListNode(head.random.label);
                cloned.random = random;
                map.put(head.random, random);
            }
            head = head.next;
            temp = temp.next; // node's next is connected by dummy
        }
        return dummyHead.next;
    }

    // method 2: clone node, next, and random pointer.
    public static RandomListNode copyRandomList2(RandomListNode head){
        if(head == null) return null;
        Map<RandomListNode,RandomListNode> map = new HashMap<>();
        RandomListNode clonedHead = null;
        while(head!=null){
            // copy node
            RandomListNode cloned = null;
            if(map.containsKey(head)){
                cloned = map.get(head);
            }else{
                cloned = new RandomListNode(head.label);
                map.put(head,cloned);
            }
            // copy node's next
            if(map.containsKey(head.next)){
                cloned.next = map.get(head.next);
            }else if(head.next!=null){
                RandomListNode node = new RandomListNode(head.next.label);
                cloned.next = node;
                map.put(head.next,node);
            }
            // copy node's random
            if(map.containsKey(head.random)){
                cloned.random = map.get(head.random);
            }else if(head.random!=null){
                RandomListNode random = new RandomListNode(head.random.label);
                cloned.random = random;
                map.put(head.random, random);
            }
            if(clonedHead == null) clonedHead = cloned;
            head = head.next;
        }
        return clonedHead;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值