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. 安装docker和更改docker镜像下载目录

    centos6.x系列: yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm yum inst ...

  2. gvim编码配置知识

    配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题     Vim / gVim 在中文 Windows 下的字符编码有两个问题: 默认没有编码检测功能 如果一个 ...

  3. 文件 I/O缓冲流

    import java.io.File; import java.io.Writer; import java.util.StringTokenizer; import java.io.Reader; ...

  4. (数据科学学习手札34)多层感知机原理详解&Python与R实现

    一.简介 机器学习分为很多个领域,其中的连接主义指的就是以神经元(neuron)为基本结构的各式各样的神经网络,规范的定义是:由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系 ...

  5. MFC接收ShellExecute多个参数

    在应用程序开发过程中,我们经常需要带参数启动另一个执行程序,如何传递多个参数,如何解析多个参数呢?   传参数 传递参数可使用ShellExecute函数,示例如下: ShellExecute(NUL ...

  6. python--基本类型之数值

    Number(数字): 数字类型创建: a = 10b = ab = 20 pint('a : 'a)pint('b : 'b) 数据类型转换: int(x,[,base]) 将 x 转换为一个整数f ...

  7. Android面试收集录 网络与加密

    1.创建Socket对象需要至少指定哪些信息? IP(或域名)和端口号 Socket socket=new Socket("www.baidu.com",80); 2.如何使用So ...

  8. 开源版本 hadoop-2.7.5 + apache-hive-2.1.1 + spark-2.3.0-bin-hadoop2.7整合使用

    一,开源软件版本: hadoop版本 : hadoop-2.7.5 hive版本 :apache-hive-2.1.1 spark版本: spark-2.3.0-bin-hadoop2.7 各个版本到 ...

  9. vi编辑图

    vi使用方法

  10. 破解PHPStrom 10 and Pycharm

    注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...