138. Copy List with Random Pointer

4 篇文章 0 订阅
1 篇文章 0 订阅

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.
Subscribe to see which companies asked this question.

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

 /*
    自己的方案:
    首先第一次遍历,复制每个node,构成了个基本的单链表
    第二次遍历:对每个node的随机后继, 使用for循环查出 它与头结点的距离,再根据这个距离在找复制的单链表上找到随机后继,指向它。
    时间复杂度O(n^2)
    严格来说会超时
  */
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if (head == null)
            return null;
        RandomListNode tmp = head;
        RandomListNode copyListhead = new RandomListNode(tmp.label);
        copyListhead.next = null;
        copyListhead.random = tmp.random == null? null:copyListhead;
        RandomListNode tmp1 = copyListhead;
        while (tmp.next != null)
        {
            tmp = tmp.next;
            tmp1.next = new RandomListNode(tmp.label);
            tmp1 = tmp1.next;
        }
        tmp1.next = null;

        tmp = head;
        tmp1 = copyListhead;
        RandomListNode start,randomNode;
        start = head;                                   
        // 用于计算每个node的随机后继,距离头结点的距离, start在计算距离时初始化为head,在根据距离再copy上后移时初始化copyListhead

        int length=0;
        while (tmp!= null)
        {
            randomNode = tmp.random;
            if (randomNode == null)
                tmp1.random = null;
            else
            {
                for (length = 0; start != randomNode; start = start.next, length++) ;
                //计算head到randomNode的距离
                start = copyListhead;
                for (int i = 0; i < length; start = start.next, i++) ;
                //根据距离到达randomNode的copy点
                tmp1.random = start;
                start = head;
            }
            tmp = tmp.next;
            tmp1 = tmp1.next;

        }

        return copyListhead;    }
}
//问题的关键:时间主要浪费在查找randomNode上,所以优化这个步骤是关键。
//这里的解决方案,使用了哈希表。
//Map中存的key为原node,value为node副本
//从而使得能够在O(N)完成
//哈希表的妙用:快速的定位资源
public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {


        Map<RandomListNode,RandomListNode> map = new HashMap<>();
        RandomListNode tmp = head,copy;
        while (tmp!=null)
        {
            map.put(tmp,new RandomListNode(tmp.label));
            tmp = tmp.next;
        }
        tmp = head;
        while (tmp!=null)
        {
            map.get(tmp).next = map.get(tmp.next);
            map.get(tmp).random = map.get(tmp.random);
            tmp = tmp.next;
        }

        return map.get(head);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值