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.

随机链表的节点数据结构

struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};

注意本题是对带有随机链表指针的深度拷贝,

如果数据结构中那个没有随机指针,只需要将链表遍历拷贝一遍即可

但题目给的数据结构含有指针,拷贝后就必须保有随机指针的关系

本题通过在每个节点之后插入一个当前节点的拷贝节点,然后让插入的节点保有当前节点随机指针的关系,最后将插入的节点从链表中剥离出来即可

主要分为3步

(1)插入拷贝的节点

(2)复制random指针

(3)将插入的节点分离出来

/**
* 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) {
//copy list
RandomListNode *p = head;
while(p){
RandomListNode *tmp = new RandomListNode(p->label);
tmp->next = p->next;
p->next = tmp;
p = tmp->next;
}
//copy random pointer
RandomListNode *q = head;
while(q){
RandomListNode *tmp = q->next;
if(q->random){
tmp->random = q->random->next;
}
q=tmp->next;
}
//seperate list
RandomListNode *dupHead = head == NULL ? NULL : head->next, *cur = head;
while(cur){
RandomListNode *tmp = cur->next;
cur->next = tmp->next;
if(tmp->next){
tmp->next = tmp->next->next;
}
cur = cur->next;
}
return dupHead;
}
};

Leetcode Copy List with Random Pointer的更多相关文章

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

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

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

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

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

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

  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(带random引用的单链表深拷贝)

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

  6. [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝

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

  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 &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  9. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

随机推荐

  1. MyBatis学习总结(一)——MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  2. HTML5学习笔记(持续更新中....)

    平时的工作中,不知不觉我们应用了很多HTML5,但当正儿八经问起来你对HTML5了解多少,很多时候都有点懵. 做个简单的HTML5总结.包括简介.要学的知识点.凌乱的知识点 HMTL5简介 定义:ht ...

  3. CSS3中的动画功能(一)

    css3中的动画功能分为transitions功能和animations功能,这两种功能都可以通过改变css属性值来产生动画效果.今天带大家一起来看看css3动画功能中的transitions的用法. ...

  4. 文件夹锁定(Source)

    文件夹锁定(Source)private void Lock(string folderPath){    try    {        string adminUserName = Environ ...

  5. Druid初步学习

    Druid是一个JDBC组件,它包括三部分:  DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可管理的数据库连接池 ...

  6. win7提示“User Profile Service服务未能登录”

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 最近,有个同事打电话告诉我说他的用户名无法登陆到系统,提示“User Profile Service服务未能登录,无法加载用户配置文 ...

  7. #define #include #undef的其中一个用法(目的)

    一.背景 最近在跟一段系统级的代码,和原来单纯的下位机代码相比,真的是刘姥姥进大观园--看花了眼.相较于 之前所常见的各种下位机代码,系统级代码常常会出现深层次结构体嵌套,结构体内的各种回调函数导致对 ...

  8. svn清除已保存的用户名和密码

    在项目中使用SVN是必须的,我们一般将用户名和密码进行保存处理,这样做的好处在于每次都不用输入了,方便快捷.但是当我们想用另外一个svn账号时,这时候该怎么办呢,看下图,让提示框重新出来. 找到这个页 ...

  9. OS X EI Capitan安装refind时出现Could not set boot device property: 0xe00002bc

    参考:terminal - OSX 10.11 El Capitan - setting boot device property not working ... 解决办法: 1.重启MacMini, ...

  10. Java NIO 系列教程

    http://www.iteye.com/magazines/132-Java-NIO