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. 菜鸟学Linux - Hard Link与Symbolic Link

    在学习Hard Link与Symbolic Link之前,需要大概了解一下inode与data block.在Linux的文件系统中,一个文件对应一个inode与若干个data block.inode ...

  2. #include "*.c"文件的妙用

    在看uCOS II V2.91版本源代码时,在ucos_ii.c源文件中发现下面的代码: #include <os_core.c> #include <os_flag.c> # ...

  3. Codeforces 35E Parade 扫描线

    题意: 给出\(n\)个底边在\(x\)轴上的矩形,求外面的轮廓线顶点. 分析: 将每个矩形拆成两个事件:\(\\\{ l, y, + \\\}\)和\(\\\{ r, y, - \\\}\)分别表示 ...

  4. TCP/IP网络编程之多线程服务端的实现(一)

    为什么引入线程 为了实现服务端并发处理客户端请求,我们介绍了多进程模型.select和epoll,这三种办法各有优缺点.创建(复制)进程的工作本身会给操作系统带来相当沉重的负担.而且,每个进程有独立的 ...

  5. ogre3D学习基础19 --- 材质的继承,纹理的滚动与旋转

    以上一节为基础,废话不多说. 首先新增一个节点,用于比较显示 //新增一个节点 ent = mSceneMgr->createEntity("Quad"); ent-> ...

  6. 静态文本框控件的美化CStatic

    VC通用控件都是灰色,当对程序界面进行美化时,使用通用控件就和美化后的程序界面不搭配,在VB,C#中,可以很方便的更改控件背景颜色,但在VC中就不能,需要我们自己来完善这方面的功能.我在这只简单的介绍 ...

  7. (转载) Linux五种IO模型

    转载:http://blog.csdn.net/jay900323/article/details/18141217     Linux五种IO模型及分析   目录(?)[-] 概念理解 Linux下 ...

  8. Arcengine 基本操作(待更新)

    /// <summary> /// 删除fieldName属性值为1的弧段 /// </summary> /// <param name="fieldName& ...

  9. BZOJ3524 [Poi2014]Couriers 【主席树】

    题目 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入格式 第一 ...

  10. java简易DVD影片管理系统—面向对象

    public class DvdSet { String name [] =new String[15]; // DVD电影名称 String date [] =new String[15]; //D ...