LeetCode OJ--Copy List with Random Pointer **
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 **的更多相关文章
- [LeetCode OJ] Copy List with Random Pointer 扩大
职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- [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 ...
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- LeetCode _ Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 【 Copy List with Random Pointer 】 python 实现
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- python3+openCV实现图片的人脸人眼检测,原理+参数+源代码
上学时候用matlab学过一些图像处理的基础知识,当时课程作业是用haar实现人脸检测 but当时是心思根本不在图像处理上,so找了个同学帮忙做的,自己没上心 然鹅天道好轮回,现在捡起来了原来的算法一 ...
- 并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- P1880 [NOI1995]石子合并【区间DP】
题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...
- Spark性能优化:开发调优篇
1.前言 在大数据计算领域,Spark已经成为了越来越流行.越来越受欢迎的计算平台之一.Spark的功能涵盖了大数据领域的离线批处理.SQL类处理.流式/实时计算.机器学习.图计算等各种不同类型的计算 ...
- Oracle数据库迁移--->从Windows到Linux
I did a practice to migrate the oracle database from windows to linux operation system. The followin ...
- Apache shiro学习总结
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- C#实现eval 进行四则运算(有码)
在JavaScript中实现四则运算很简单,只需要调用eval函数就行了,但是不知道什么原因万能的.NET却没有封装这个函数~ 在这里为大家封装了一个C#版本的eval函数,具体的设计参考了<大 ...
- 设计模式之序章-UML类图那点事儿
设计模式之序-UML类图那点事儿 序 打14年年底就像写那么一个系列,用于讲设计模式的,代码基于JAVA语言,最早接触设计模式是大一还是大二来着,那时候网上有人给推荐书,其中就有设计模式,当时给我推荐 ...
- day02_02.能被3整除的个位数为6的数
第2题 能被3整除的个位数为6的数 难度增加一点点,再接再厉 注意: 把一些限制条件,用PHP编程的语言来执行 题目:输出100以内(不含100)能被3整除且个位数为6的所有整数 <?php f ...
- SVD简化数据
一,引言 我们知道,在实际生活中,采集到的数据大部分信息都是无用的噪声和冗余信息,那么,我们如何才能剔除掉这些噪声和无用的信息,只保留包含绝大部分重要信息的数据特征呢? 除了上次降到的PCA方法,本次 ...