题目:复制带随机指针的链表



给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。

返回一个深拷贝的链表。

您在真实的面试中是否遇到过这个题?

Yes





样例

挑战

可否使用O(1)的空间
标签 Expand   



相关题目 Expand    

看似普通的链表操作,只是多了一个随机指针。
但是在遍历深度拷贝时候,会出现随机指针并不存在的情况,这就无法去指向随机指针。
但是可以用下面这种方式
                                       --------------------------------------|           
1.原始链表:1------->  2  ------->3---------->4----------->5  
                      -----------------------^
               红色表示random指针。
2.在原始链表后复制原始链表的节点:
                                  --------------------------------------------------------------------------------------------|  
1 ------->  1 ------->2 ---------->2 ----------->3 ---------> 3 ------->  4   ------->4 ---------->5 ----------->5 
  --------------------------------------------------^
3. 复制原始链表的random指针
cur.next.random = cur.random.next;
                                                       ------------------------------------------------------------------------------|  
                                  -------------------------------------------------------------------------------|  
1 ------->  1  ------->2 ---------->2 ----------->3  ---------> 3 ------->  4   ------->4 ---------->5 ----------->5 
  --------------------------------------------------^
                  ----------------------------------------------------^
4.将新旧链表的节点分离。

/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
*     int label;
*     RandomListNode next, random;
*     RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
    /**
     * @param head: The head of linked list with a random pointer.
     * @return: A new head of a deep copy of the list.
     */
    public RandomListNode copyRandomList(RandomListNode head) {
        // write your code here
         if(null==head) return null;
         /*复制原始链表*/
         RandomListNode cur = head;
         while(null!=cur){
              RandomListNode newcur = new RandomListNode(cur.label);
              newcur.next  = cur.next;
              cur.next = newcur;
              cur = cur.next.next;
         }        
         /**复制链表的random指针**/
         cur = head;
         while(null!=cur){
              if(null!=cur.random){
                  cur.next.random = cur.random.next;
              }
              cur = cur.next.next;
         }
        
         /**拆分新旧链表**/
         cur = head;
         RandomListNode newhead  = head.next;
         while(null!=cur){
             RandomListNode newcur = cur.next;
              if(null!=newcur){
                   cur.next = newcur.next;
              }
            if (newcur.next != null) newcur.next = newcur.next.next; 
              cur = cur.next;
         }
         return newhead;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值