题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer

思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom->next 就是新链表的random指针

所以分3步骤:

1 新元素插入

2 设置新链表的random

3 拆分大链表,回复old link 和new link

 /**
* 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) { RandomListNode* pOld = head;
RandomListNode* pNew = NULL;
RandomListNode* pRes = NULL; if(head == NULL) return NULL; // insert every new node after old new node
while(pOld)
{
pNew = new RandomListNode(pOld->label);
if(pOld == head) pRes = pNew;
pNew->next = pOld->next;
pOld->next = pNew;
pOld = pNew->next;
} pOld = head;
// constrct new's random pointer
while(pOld)
{
pNew = pOld->next;
if(pOld->random == NULL)
pNew->random == NULL;
else
pNew->random = pOld->random->next;
pOld = pNew->next;
} // recover old's and new's next pointer
//恢复old list 和new list pOld = head; while(pOld)
{
pNew = pOld->next;
if(pNew == NULL)
pOld->next = NULL;
else
pOld->next = pNew->next;
pOld = pNew;
} return pRes;
}
};

[LeetCode] Copy List with Random Pointe的更多相关文章

  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

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

  9. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

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

随机推荐

  1. HttpClient通过Post上传文件(转)

    在之前一段的项目中,使用Java模仿Http Post方式发送参数以及文件,单纯的传递参数或者文件可以使用URLConnection进行相应的处理. 但是项目中涉及到既要传递普通参数,也要传递多个文件 ...

  2. Java 8新特性——default方法(defender方法)介绍

    我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: 1 2 3 4 5 6 7 8 9 10 11 ...

  3. ASP.NET Web API路由规则(二)

    默认的规则 在ASP.NET MVC4中 global.asax.cs代码中并无注册默认路由规则的代码 代码如下: public class WebApiApplication : System.We ...

  4. MemCached配置与缓存知识概述

    先看看百度百科里面对缓存的介绍: 缓存(Cache memory)是硬盘控制器上的一块内存芯片,具有极快的存取速度,它是硬盘内部存储和外界接口之间的缓冲器.由于硬盘的内部数据传输速度和外界介面传输速度 ...

  5. Objective-c基础学习

    核心内容 标识号 OC语言中,对各种变量,方法和类等要素命名时使用的字符序列称为标识符. OC标识符命名规则标识符由字母,下划线“_”,美元符号“$”和数字组成,标识符必须以字母,下划线,美元符号开头 ...

  6. [MetaHook] Load TGA texture to OpenGL

    This function load a *.tga texture file and convert to OpenGL pixel format, uncompress only. #pragma ...

  7. 启动页面设置,icon图标设置

    更多尺寸像素如何放置请看:http://chicun.jammy.cc/ 如何设置App的启动图,也就是Launch Image? 新建一个iosLaunchImage文件夹

  8. 20145222黄亚奇《Java程序设计》第6周学习总结

    教材学习内容总结 第十章 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度来看,如果要将数据从来源取出,可以使用输入串流,如果要将数据写入目的地,可以使用输 ...

  9. Spring Cache和MyBatis的使用

    1.http://www.importnew.com/22757.html    spring chache缓存的使用. 2.http://www.importnew.com/22783.html   ...

  10. jdbc基础 (一) MySQL的简单使用

    前段时间学习了jdbc,正好利用这几篇文章总结一下. JDBC 可做三件事:与数据库建立连接.发送操作数据库的语句并处理结果. 而程序首先要做的就是加载数据库驱动,这里我使用的是mysql: Stri ...