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 pointer属性。所以有点懵,大神的做法如下,加入个人理解。

思路:对链表进行三次遍历。

第一次遍历复制每一个结点,将新结点接在原结点的后面, 让链表变成一个重复链表,新旧交替;

第二次遍历维护新结点的随机指针,因,新结点是在旧结点之后,所以node->next->random=node->random->next,而node->node结点为新结点,这样,我们就把新结点的随机指针接好了;

第三次遍历,将两新旧结点分开,因为,构成的重复链表是新旧结点交替出现,故,只要每隔一个节点相连即可,就完成了对链表的分割。如下图:

 /**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head)
{
if(head==NULL) return NULL; //第一步,在oldNode的后面插入一个新的结点
RandomListNode *oldNode=head;
while(oldNode !=NULL)
{
RandomListNode *newNode=new RandomListNode(oldNode->label);
newNode->next=oldNode->next;
newNode->random=oldNode->random;
oldNode->next=newNode;
oldNode=newNode->next; //遍历前行
} //第二步,关联random
oldNode=head;
while(oldNode !=NULL)
{
if(oldNode->random !=NULL)
oldNode->next->random=oldNode->random->next;
oldNode=oldNode->next->next;
} //第三步,分开两链表
RandomListNode *newList=new RandomListNode();
newList->next=head;
RandomListNode *pHead=newList; oldNode=head;
while(oldNode !=NULL)
{
pHead->next=oldNode->next;
oldNode->next=pHead->next->next; pHead=pHead->next;
oldNode=oldNode->next;
} return newList->next;
}
};

[Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝的更多相关文章

  1. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

  2. 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现

    题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...

  3. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  4. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  6. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  7. LeetCode——Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  9. LeetCode – Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

随机推荐

  1. Java源码解析——集合框架(一)——ArrayList

    ArrayList源码分析 ArrayList就是动态数组,是Array的复杂版本,它提供了动态的增加和减少元素.灵活的设置数组的大小. 一.类声明 public class ArrayList< ...

  2. ionic 做移动应用怎么样?

    看了很多网上的赞美性介绍后,我们选用了这个做开发,目前碰到的坑有: android, list界面上下滑动会lag ios下,当键盘弹出时,你可以选择整个页面scroll,也可以选择不scroll,但 ...

  3. Kubernetes-深入分析集群安全机制

    Kubernetes过一系列机制来实现集群的安全机制,包括API Server的认证授权.准入控制机制及保护敏感信息的Secret机制等.集群的安全性必须考虑以下的几个目标: 保证容器与其所在宿主机的 ...

  4. [BZOJ1040][ZJOI2008]骑士(树形DP)

    对于一个联通块内,有且只有一个环,即n个点n条边 那么找到那个环,然后任意断一条边,这个联通块就变成一棵树了,然后做树形DP就行了 对于断的边要记录下来DP时特判 Code #include < ...

  5. WebService第一天——概述与入门操作

    一.概述 1.是什么 Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些 ...

  6. Xcode9新变化

    http://www.cnblogs.com/lurenq/archive/2017/09/26/7594909.html

  7. java中array,arrayList,iterator;

    Array        String []a = new String[10] ;  a[0] = "test" ; String []a = new String[]{&quo ...

  8. Hbase读写流程和寻址机制

    写操作流程 (1) Client通过Zookeeper的调度,向RegionServer发出写数据请求,在Region中写数据. (2) 数据被写入Region的MemStore,直到MemStore ...

  9. 指纹识别人脸识别 iOS

    //1.判断iOS8及以后的版本 if([UIDevice currentDevice].systemVersion.doubleValue >= 8.0){ //从iPhone5S开始,出现指 ...

  10. ionic 向路由中的templateUrl(模板页)传值

    .state('product', { url: '/product/:id', templateUrl: function ($routeParams) { return '/Product/Ind ...