[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 ...
随机推荐
- MySQL的边角料
//1查询对应数据库所有的表 SELECT * FROM information_schema.`TABLES` WHERE TABLE_SCHEMA ="数据库名" 2 查询数据 ...
- discuzX3.2 X3.4网站漏洞修复 SQL注入与请求伪造攻击利用与修复
2018年12月9日,国内某安全组织,对discuz X3.2 X3.4版本的漏洞进行了公开,这次漏洞影响范围较大,具体漏洞是discuz 的用户前段SQL注入与请求伪造漏洞,也俗称SSRF漏洞,漏洞 ...
- Waterline从概念到实操
Waterline基本介绍 Waterline是什么 Waterline是下一代存储和检索引擎,也是Sails框架中使用的默认ORM . ORM的基本概念 Object Relational Mapp ...
- List排序方法
可用使用Collections.sort(List<T> list)和Collections.sort(List<T> list, Comparator<? super ...
- flask与javascript及ajax
flask与javascript及ajax 1. flask+js 1.1. 最简单的 最简单的元素信息改变. {% block content %} <h1>我的第一张网 ...
- RTSC和XDCTool的理解
1. 在使用TI的开发工具CCS中,里面有几个重要的概念,一直不太清晰,RTSC是什么,XDCTool是什么?包是什么?包的版本为啥都是4位的(比如mathlib_c66x_3_0_1_1)?star ...
- python爬取数据需要注意的问题
1 爬取https的网站或是接口的时候,如果是不受信用的SSL证书,会报错,需要添加如下代码,如下代码可以保证当前代码块内所有的请求都自动屏蔽ssl证书问题: import ssl # 这个是爬取ht ...
- 对工具的反思 & deadlines与致歉
人和动物最大的区别就是使用工具的水平. 有些人只凭着对工具的熟练掌握便成了牛人. 工具,到底应该以何种态度去看待? 在我小的时候,工具仅仅是指树枝.线.粉笔,可以让自己有更多游戏可玩:上学之后,便又有 ...
- Tensorflowonspark安装
1.实验环境 Centos7+Python3.6+Java8+Hadoop2.6+Spark2.3+Tensorflow1.10.0 2.Tensorflow安装 最简单的方式:pip install ...
- [C/C++] new/delete和malloc/free基本区别
/**便于遗忘时复习**/ 区别一:本质 new/delete 在C++中是运算符不是函数,需要编译器支持.malloc/free是库函数,需要头文件支持,在C语言中使用. 区别二:开辟内存大小 用 ...