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. Redis之String类型操作

    接口IRedisDaoStr: package com.net.test.redis.base.dao; import java.util.List; import java.util.Map; /* ...

  2. angularjs的使用技巧

    1. AngularJS的module函数有两种用法, a. 定义一个module, 需要传入2个参数,module('moduleName', []), 第一个参数是新的module名称,第二个参数 ...

  3. models管理类抽取基类

    Models类 models.py # coding:utf-8 from django.db import models from db.Base_model import Base_Model f ...

  4. loj2291 「THUSC 2016」补退选

    ref pkusc 快到了,做点 thusc 的题涨涨 rp-- #include <iostream> #include <cstring> #include <cst ...

  5. ffmpeg转换参数和对几种视频格式的转换分析

    我们在将多种格式的视频转换成flv格式的时候,我们关注的就是转换后的flv视频的品质和大小.下面就自己的实践所得来和大家分享一下,主要针对avi.3gp.mp4和wmv四种格式来进行分析.通常在使用f ...

  6. Web 安全问题 rel="noopener nofollw"

    1. noopener 如果你需要用 a 标签打开一个标签页时,你会使用 target='_blank' 这个属性,此时你需要添加 rel='noreferrer noopener' 当你使用 tar ...

  7. 理解机器为什么可以学习(四)---VC Dimension

    前面一节我们通过引入增长函数的上限的上限,一个多项式,来把Ein 和 Eout 的差Bound住,这一节引入VC Bound进一步说明这个问题. 前边我们得到,如果一个hypethesis集是有bre ...

  8. c# 钩子程序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  9. shell执行mysql的脚本(包括mysql执行shell脚本)

    在Shell中执行mysql的脚本,这里介绍比较容易使用的一种方法 首先写好sql的脚本,后缀为.sql,比如 sql_file.sql:内容如下 #这是SQL的脚本create table if n ...

  10. Summary—【base】(JavaScript)

    1.认识Js     1.1 Js就是一门运行在客户端浏览器的脚本编程语言     1.2 组成            ECMAScript:Js的语法标准            DOM:JS操作网页 ...