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位置,再拆分两个链表。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; // 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; RandomListNode * p = head; //在每个结点后面复制一个自己 不管random指针
while(p != NULL)
{
RandomListNode * cpy = new RandomListNode(p->label);
cpy->next = p->next;
p->next = cpy; p = cpy->next;
} //复制random指针
p = head;
while(p != NULL)
{
RandomListNode * cpy = p->next;
if(p->random != NULL)
{
cpy->random = p->random->next;
} p = cpy->next;
} //把原链表与复制链表拆开
RandomListNode * cpyhead = head->next;
p = head;
RandomListNode * cpy = cpyhead;
while(p != NULL)
{
p->next = cpy->next;
cpy->next = (cpy->next == NULL) ? cpy->next : cpy->next->next; p = p->next;
cpy = cpy->next;
} return cpyhead;
}
}; int main()
{
RandomListNode * r = new RandomListNode(-);
Solution s;
RandomListNode * ans = s.copyRandomList(r); return ;
}

【leetcode】Copy List with Random Pointer (hard)的更多相关文章

  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】【Hard】Copy List with Random Pointer

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

  3. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  4. [Leetcode Week17]Copy List with Random Pointer

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

  5. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

  6. Java for LeetCode 138 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] 138. 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] 138. 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 138. Copy List with Random Pointer ----- java

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

随机推荐

  1. Windows 操作小技巧 之一(持续更新)

    1.图片批量旋转 通常携带单反去景点排了大量照片回来处理图片时都会遇到很多横竖杂乱排序的图片难以处理的情形.现提供如下技巧进行处理. 1).在文件夹中添加"方向"的排列或分组选项: ...

  2. spring学习

    http://blog.csdn.net/chjttony/article/details/6301523 http://blog.csdn.net/chjttony/article/details/ ...

  3. CSS vertical-align 属性

    定义和用法 vertical-align 属性设置元素的垂直对齐方式.该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐

  4. zstu.2512. Moving Tables(贪心)

     Moving Tables Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1182  Solved: 563 Description The famo ...

  5. linux 中断理解

    1.进程.线程只针对的是应用层,而内核调用.驱动没有这种概念,调用的都是内核调用里相同的函数或变量,所以应用层多个应用操作同个硬件时,特别是要加互斥操作,8250通过cs针脚决定发送数据给哪个串口 2 ...

  6. java之远程接口调用

    一.通过地址栏传值 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  7. tomcat中配置jmx监控

    1.在tomcat的start.bat中添加下面代码, -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote. ...

  8. VVDocumenter升级后不能使用问题

    VVDocumenter-Xcode是Xcode上一款快速添加标准注释,并可以自动生成文档的插件.有了VVDocumenter-Xcode,规范化的注释,只需要输入三个斜线“///”就可以搞定,非常方 ...

  9. Caffe学习系列(11):数据可视化环境(python接口)配置

    参考:http://www.cnblogs.com/denny402/p/5088399.html 这节配置python接口遇到了不少坑. 1.我是利用anaconda来配置python环境,在将ca ...

  10. jQuery lazyload插件详解和问题解答

    lazyload插件用于图片延迟加载,节省服务器带宽,减少服务器请求次数,提高网站的页面加载速度,用于网站性能优化,只有当图片在窗口可视范围内时才向服务器请求: 参数: threshold:设置距离窗 ...