[Leetcode] 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 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 对带有任意指针的链表深度拷贝的更多相关文章
- [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表
public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...
- 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- Leetcode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [leetcode]Copy List with Random Pointer @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- LeetCode——Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- LeetCode – Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- (数据科学学习手札27)sklearn数据集分割方法汇总
一.简介 在现实的机器学习任务中,我们往往是利用搜集到的尽可能多的样本集来输入算法进行训练,以尽可能高的精度为目标,但这里便出现一个问题,一是很多情况下我们不能说搜集到的样本集就能代表真实的全体,其分 ...
- SKIP(插入空行)
WRITE 'This is the 1st line'. SKIP. WRITE 'This is the 2nd line'. 跳转至某一行 SKIP TO LINE line_number. 插 ...
- shell重温---基础篇(shell数组&数组操作)
上篇博客已经分析重温了shell的运行方式以及其中的变量还有字符串,之后按照套路就是数组方面了,废话不多说,直接进入正题哈.(小白笔记,各位看官勿喷...) bash shell呢,支持一位数 ...
- Ehcache缓存实例
一:目录 EhCache 简介 Hello World 示例 Spring 整合 Dummy CacheManager 的配置和作用 二: 简介 1. 基本介绍 EhCache 是一个纯Java的进程 ...
- JS 实现随机验证码功能
1.验证码 验证是网页常出现的一个验证点,所谓验证码类型有很多,下面代码只是实现一个简单的验证功能. <div> <input type = "text" id ...
- Python的类(二)
一.类的重写 对于父类的方法,只要它不符合子类模拟的实物的行为,都可对其进行重写.为此,可在子类中定义一个这样的方法,即它与要重写的父类方法同名.这样, Python将不会考虑这个父类方法,而只关注你 ...
- SXOI2018游记
day0 动身去太原.太原五中虽然挺小的但是很好看啊qwq(进门口一个"通天堂"(逃 试机.似乎看到了__stdcall!!然而没敢去认orz.linux选手似乎是9个.准考证(一 ...
- 【APUE】Chapter1 UNIX System Overview
这章内容就是“provides a whirlwind tour of the UNIX System from a programmer's perspective”. 其实在看这章内容的时候,已经 ...
- 剑指offer-斐波那契数列07
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 class Solution: def Fibonacci(self ...
- [Linux] 服务器镜像定时备份解决方案 crontab+rsync+flock
两台服务器定时同步文件解决方案: 环境: 主机:192.168.1.1 镜像机:192.168.1.2 需要将主机内容备份至镜像机(假设用户都为root) 备份内容为 /export 目录下所有内容至 ...