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. 百度地图API定位+显示位置

    1. 先在需要嵌入地图的页面引入map.js <script src="http://api.map.baidu.com/api?v=2.0&ak=你的秘钥"> ...

  2. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column …… 出现这个异常的很大可能性是 数据库是没有问题的 ...

  3. SVN配置自启动-1053错误

    主要内容:解决启动“配置的svn自启动服务”报1053错误 1. 环境: 系统: wind10 svn服务端版本: VisualSVN-Server-3.8.0-x64 2. 配置自启动 以管理员身份 ...

  4. JS 红包随机

    微信随机红包,指定金额指定用户,随机发送红包 var moneys = new Array(); var moneyTotal = 0; function rand(obj){ if(obj.size ...

  5. python学习之路2(程序的控制结构)

    1.程序的分支结构 1.1 单分支 if <条件>:                       例:guess = eval(input()) <语句块>          ...

  6. idea添加源代码目录,编译代码出现时钟样式

    项目结构需要有一个target目录,需要一个src目录,

  7. Python2 Sequence类型簇

  8. 学习CSS

    CSS教程 菜鸟教程 通过使用CSS我们可以大大提升网页开发的工作效率 什么是CSS? CSS指层叠样式表(Cascading Style Sheets) 样式定义如何显示HTML元素 样式通常存储在 ...

  9. itchat和matplotlib的结合使用爬取微信信息

    前几天无意中看到了一片文章,<一件有趣的事:我用 Python 爬了爬自己的微信朋友>,这篇文章写的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵称.性别.地理位 ...

  10. 数据库 MySQL part2

    表记录的操作 增 1.插入一条记录 语法:insert [into] tab_name (field1,filed2,.......) values (value1,value2,.......); ...