JAVA学习44_leetCode解题报告之Copy List with Random Pointer

本文介绍一种高效算法来深拷贝一个带有额外随机指针的链表,该算法通过三个步骤确保新链表与原链表完全独立且正确地复制了随机指针的链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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.

分析:

我们知道如果是简单的copy List 的话,那么我们只需要从头到尾遍历下来,new出对应个数的Node,并把它们的连接关系设置好就可以了,但是这道题目中每个节点Node出现了Random属性,也就意味着可能当前结点Node所依赖的那个Random对应的结点还没有被创建出来。


解题思路:

为了解决“分析”里提到的问题,我们需要做如下三步处理!

1. 在OldList中的每个结点后,插入一个CopyNode,这个结点的Random域和Next域与OldList中的被拷贝Node的Random域和Next一致,然后让被拷贝结点的Next域指向CopyNode结点,这样先创建出OldList中结点对应的CopyNode结点。

2. 由于所有的CopyNode都已经创建出来了,我们就可以调整这些CopyNode真正的Random域的值了。

3. 调整所有CopyNode的Next域的值,恢复OldList所有Node的Next的值到初始状态!


图解:



AC代码:


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package copylist;  
  2. class RandomListNode {  
  3.     int label;  
  4.     RandomListNode next, random;  
  5.     RandomListNode(int x) { this.label = x; }  
  6. };  
  7.   
  8. public class Solution {  
  9.     public RandomListNode copyRandomList(RandomListNode head) {  
  10.           
  11.         if (head == null)  
  12.             return head;  
  13.         /*第一步:在OldList的每个节点后面都插入一个copyNode(拷贝链表的结点)*/  
  14.         RandomListNode nowNode = head;  
  15.         while (nowNode != null){  
  16.             RandomListNode copyNode = new RandomListNode(nowNode.label);  
  17.             copyNode.random = nowNode.random;  
  18.             copyNode.next = nowNode.next;  
  19.             nowNode.next = copyNode;  
  20.             nowNode = nowNode.next.next;  
  21.         }  
  22.           
  23.         /*第二步:确定NewList的每个节点,真正关联到的Random结点是哪个, 
  24.          *      因为第一步已经把所有NewList上的结点都建立了*/  
  25.         nowNode = head;  
  26.         while (nowNode != null){  
  27.             if (nowNode.random != null){  
  28.                 nowNode.next.random = nowNode.random.next;  
  29.             }  
  30.             nowNode = nowNode.next.next;  
  31.         }  
  32.           
  33.         /*第三步:还原OldList的next为一开始的next结点 
  34.          *      并拼接NewList的next到它真正所应该关联的next结点 
  35.          *      即:保持老链表OldList不变,拼接新链表NewList! 
  36.          * */  
  37.         RandomListNode pHead = new RandomListNode(0);  
  38.         pHead.next = head;  
  39.         RandomListNode newlist = pHead;  
  40.           
  41.         nowNode = head;  
  42.         while (nowNode != null){  
  43.             pHead.next = nowNode.next;  
  44.             nowNode.next = pHead.next.next;  
  45.             pHead = pHead.next;  
  46.             nowNode = nowNode.next;  
  47.         }  
  48.         return newlist.next;  
  49.     }  
  50. }  

原文来自:http://blog.youkuaiyun.com/ljphhj/article/details/21832129?utm_source=tuicool&utm_medium=referral

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值