Lintcode105 Copy List with Random Pointer solution 题解
【题目描述】
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.
给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。
返回一个深拷贝的链表。
【题目链接】
www.lintcode.com/en/problem/copy-list-with-random-pointer/
var data = {
val:1,
next:{
val:2,
next:{
val:3,
next:{
val:4,
next:null
}
}
}
};
data.rand = data.next.next;
data.next.rand = null;
data.next.next.rand = null;
data.next.next.next.rand = data;
const copyRandomList = function(head){
if(head === null){
return null;
}
let cur = head;
let next = null;
while(cur !== null){
next = cur.next;
cur.next = {};
cur.next.next = next;
cur = next;
}
cur = head;
let curCopy = null;
while(cur !== null){
next = cur.next.next;
curCopy = cur.next;
curCopy.val = cur.val;
curCopy.rand = (function(){
if(cur.rand !== null){
// cur.rand是正本,cur.rand.next是副本,所以返回的是副本指向
return cur.rand.next;
}
return null;
})();
cur = next;
}
let res = head.next;
cur = head;
while(cur !== null){
next = cur.next.next;
curCopy = cur.next;
cur.next = next;
curCopy.next = (function(){
if(next !== null){
return next.next;
}
return null;
})();
cur =next;
}
return res;
};
var result = copyRandomList(data);
console.log(result);
Lintcode105 Copy List with Random Pointer solution 题解的更多相关文章
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- 【Lintcode】105.Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [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(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
随机推荐
- 小程序里let和var以及const区别
在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. [JavaScript] 纯文 ...
- java中的静态代理和动态代理
1.动态代理的定义:为其他对象提供一个代理以控制对这个对象的访问 代理类主要负责委托类的预处理消息,过滤消息,把消息传给委托类以及消息事后处理 按照代理类的创建时期,代理类可以分为2种:静态代理类(在 ...
- Manjaro 玩机记录
需求: 物理机使用linux个人版本系统,最好支持 微软office QQ/Tim 等通讯软件, 软件易安装, 图形界面可修改, 具有多个多个开发环境如:python2 python3 gcc nod ...
- Python cffi学习
cffi是连接Python与c的桥梁,可实现在Python中调用c文件.cffi为c语言的外部接口,在Python中使用该接口可以实现在Python中使用外部c文件的数据结构及函数. 由于资料较少,所 ...
- Jmeter学习之-从数据库取出数据并且校验数据是否准确
https://www.cnblogs.com/wuyonghuan/p/7479582.html 应用场景:调用某个接口像数据库中插入数据,需要在接口调用完成后查看数据更新或插入的数据是否正确的时候 ...
- jQuery Mobile的默认配置项具体解释,jQuery Mobile的中文配置api,jQuery Mobile的配置说明,配置大全
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xmt1139057136/article/details/35258199 学习jQuery Mob ...
- 20190422 SQL SERVER 服务
-- 数据库服务-- SQL Server(MSSQLSERVER)是必须要开启的,这个是数据库引擎服务,就像汽车的发动机一样-- SQL Server代理(MSSQLSERVER)是代理服务,比如你 ...
- sql语句格式化数字(前面补0)
将一个数字例如33,或1使用t-sql语句转换成033或001 以下是详细分析: .,)得到1000 . as varchar) 将1000转换类型 .,) 从右边取3个字符得到033 将1格式化同上 ...
- mysql插入数据会变中文
db.url=jdbc:mysql://192.168.0.149:3306/pack_platform_dev?useUnicode=true&characterEncoding=utf-8 ...
- [OpenCV]直线拟合
OpenCV实现了直线的拟合. CV_IMPL void cvFitLine( const CvArr* array, int dist, double param, double reps, dou ...