138. 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.

深拷贝带有random指针的linkedlist 第一种解法应该挺好想的

深度拷贝一个普通链表的算法非常简单,因为每一个节点只有一个next指针指向下一个节点,整体上是一个线性结构;但是题目中的节点多了一个随机指针,可以指向链表中的任何节点,或者是空的节点。所以在建立新的链表的时候,要考虑到随机指针指向的节点是否已经被新建过了,是否已经存在于新链表中。
1. 需要查重的时候,自然想到了散列表(哈希表),所以每次新建一个节点时,都将原链表里的节点和新的节点放入HashMap中,以原节点为键,新节点为值,这样在复制next指针指向的节点、或者是random指针指向的节点时,都可以在O(1)的时间内在散列表中找到已经复制过并放入新链表中的新节点。这个算法的时间复杂度是O(N),额外空间复杂度是O(N)。
public RandomListNode copyRandomList(RandomListNode head) {
  if (head == null) return null;
  
  Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
  
  // loop 1. copy all the nodes
  RandomListNode node = head;
  while (node != null) {
    map.put(node, new RandomListNode(node.label));
    node = node.next;
  }
  
  // loop 2. assign next and random pointers
  node = head;
  while (node != null) {
    map.get(node).next = map.get(node.next);
    map.get(node).random = map.get(node.random);
    node = node.next;
  }
  
  return map.get(head);
}

2. 使用散列表虽好,但是会占用O(N)的额外空间,如果可以使用常数级别的额外空间就最好了。方法一中占用空间的是新建的散列表HashMap,如果可以用别的方法将原节点与新节点联系起来,在O(1)的时间内可以找到对应的节点,就可以不使用散列表了。因为题目中已经告知随机指针只会指向链表中的节点或者是空节点,对于这一特性,每次在建立新节点时,将新节点插入到旧链表中相应的节点后面,如旧链表1->2->3->4,在遍历和插入之后就会变成1->1'->2->2'->3->3'->4->4',而第二次遍历时将新节点里的random指针指向旧节点random指针指向的节点的next,如果在链表1->1'->2->2'->3->3'->4->4'中,节点4的random指针指向了节点1,那么就让节点4的next (4')的random指向节点1的next (1');最后再遍历链表,将新的节点都取出来,组成新的链表。这个算法的时间复杂度是O(3N) = O(N),额外空间复杂度是O(1)。
public RandomListNode copyRandomList(RandomListNode head) {
    RandomListNode iter = head, next;

    // First round: make copy of each node,
    // and link them together side-by-side in a single list.
    while (iter != null) {
        next = iter.next;

        RandomListNode copy = new RandomListNode(iter.label);
        iter.next = copy;
        copy.next = next;

        iter = next;
    }

    // Second round: assign random pointers for the copy nodes.
    iter = head;
    while (iter != null) {
        if (iter.random != null) {
            iter.next.random = iter.random.next;
        }
        iter = iter.next.next;
    }

    // Third round: restore the original list, and extract the copy list.
    iter = head;
    RandomListNode pseudoHead = new RandomListNode(0);
    RandomListNode copy, copyIter = pseudoHead;

    while (iter != null) {
        next = iter.next.next;

        // extract the copy
        copy = iter.next;
        copyIter.next = copy;
        copyIter = copy;

        // restore the original list
        iter.next = next;

        iter = next;
    }

    return pseudoHead.next;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值