https://oj.leetcode.com/problems/copy-list-with-random-pointer/

灵活的指针链表应用。

每个节点有两个指针next,random,对本链表做一个深拷贝。就是完全用新内存弄出一个一样的来。

a链表:  a b c三个node

b链表: a1 b1 c1三个node

a->next = a1

a1->next = b

这样建立关系,之后再扫一遍,对a1的random赋值,之后再扫一遍,对a1->next赋值。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; 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;
for(RandomListNode *pointer = head;pointer!=nullptr;)
{
RandomListNode *link2node = new RandomListNode(pointer->label);
link2node->next = pointer->next;
pointer->next = link2node; pointer = pointer->next->next;
}
for(RandomListNode *pointer = head;pointer!=nullptr;)
{
if(pointer->random!=NULL)
pointer->next->random = pointer->random->next;
pointer = pointer->next->next;
} RandomListNode *head2 = head->next;
RandomListNode * pointer2 = nullptr;
for(RandomListNode *pointer = head;pointer!=nullptr;pointer = pointer->next)
{
pointer2 = pointer->next->next;
if(pointer2 != nullptr)
{
pointer->next->next = pointer->next->next->next;
pointer->next = pointer2;
}
else
{
pointer->next->next = NULL;
pointer->next = NULL;
}
}
return head2;
}
};
int main()
{
RandomListNode *n1 = new RandomListNode();
RandomListNode *n2 = new RandomListNode();
RandomListNode *n3 = new RandomListNode();
RandomListNode *n4 = new RandomListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n1->random = n2;
n2->random = n1;
n4->random = n4;
class Solution mys;
mys.copyRandomList(n1);
}

LeetCode OJ--Copy List with Random Pointer **的更多相关文章

  1. [LeetCode OJ] Copy List with Random Pointer 扩大

    职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...

  2. [Leetcode Week17]Copy List with Random Pointer

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

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. 【leetcode】Copy List with Random Pointer (hard)

    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 ----- java

    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

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

  10. leetcode 【 Copy List with Random Pointer 】 python 实现

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

随机推荐

  1. Nearest Common Ancestors POJ - 1330 (LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 34657   Accept ...

  2. 【Akroma, Angel of Fury】完成svn环境搭建

    昨天的那篇博文恰恰是实验室所干的事儿 但是那是一种很投机取巧的方式完成的多project管理方式 来看看我建立环境的方法 首先,找一个比较闲的公用服务器(为什么不用自己的?有公共资源不用,你傻啊?), ...

  3. ubuntu下eclipse c++开发

    linux下eclipse运行C++程序出现Launch Failed. Binary Not Found.错误 在unbutu16.04上安装eclipse c++,运行一个hello world程 ...

  4. windows下的命令

    1.cd 现在默认只能在当前盘符中改变目录,如果要改变盘符则需要多加一个/d命令. cd /d d:/git/springTest 2.chdir 显示当前目录名或改变当前目录. CHDIR [/D] ...

  5. 【精彩回顾】第二届微医前端技术沙龙(附PPT下载)

    5 月 25 日,以「无界」为主题的第二届微医前端技术沙龙成功举办.本届沙龙的演讲题目涵盖了前端技术几个主要的应用场景,包括服务端.桌面端以及跨平台的开发.最近几年前端技术发展非常快,各种可以提高开发 ...

  6. asp.net实现调用ffmpeg实现视频格式的转换

    视频格式转换的函数 //视频转换 public void VideoConvertFlv(string FromName, string ExportName) { string ffmpeg = H ...

  7. 【Two Sum】cpp

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  8. jquery ajax return jsonresult pattern

    //javascriptvar queryParams = {    "userId": userId,    "factoryId": factoryId } ...

  9. C# 中的 #region 和 #endregion 的作用

    C#中的 #region 和 #endregion 表示一块区域,这样在 Visual Studio 中可以将这块区域的代码折叠起来,便于查看. 虽然Visual Studio 也响应大括号的折叠,但 ...

  10. "二进制" 转化为 "十六进制

    //"二进制" 转化为 "十六进制" void To_string(uint8 *dest,char * src,uint8 length) { uint8 * ...