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 ...
随机推荐
- js运用6
---恢复内容开始--- 1. 逻辑运算 || && ! || 遇到第一个为true的数字就终止并返回,如果完全没有就返回最后一个值 && ...
- C和C指针小记(十六)-动态内存分配
动态内存分配 1.1 为什么使用动态内存分配 直接声明数组的方式的缺点: 1) 声明数组必须指定长度限制.无法处理超过声明长度的数组. 2) 如果声明更大的常量来弥补第一个缺点,会造成更多的内存浪费. ...
- python摸爬滚打之day21---- 模块
1.MD5加密模块 MD5是一种不可逆的加密算法, 是安全而且可靠的. 在某些网站上能够搜到MD5解密工具, 其实并没有解密工具, 而是"撞库"的方式. 网站将一些MD5数据保存 ...
- MACD技术的高级应用--MACD与波浪
在开始分析MACD指标之前,我想我们必须先从思想上认同以下两点,否则本文的研究就没有意义. 1)趋势在一段时间内是可以把握的: 2)每个指标都有有效的时候,没有指标会始终有效.我们就是要搞清楚指标 ...
- java框架之Hibernate(2)-持久化类&主键生成策略&缓存&事务&查询
持久化类 概述 持久化:将内存中的对象持久化到数据库中的过程就是持久化.Hibernate 就是用来进行持久化的框架. 持久化类:一个 Java 对象与数据库的表建立了映射关系,那么这个类在 Hibe ...
- xshell 6评估已过期解决办法 / xftp 6 评估已过期解决办法
1.工具用途介绍 Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft Windows 平台的TELNET 协议.Xshell 通过互联网到远程主机的安全 ...
- 2018-2019-1 20189221 《Linux内核原理与分析》第九周作业
2018-2019-1 20189221 <Linux内核原理与分析>第九周作业 实验八 理理解进程调度时机跟踪分析进程调度与进程切换的过程 进程调度 进度调度时机: 1.中断处理过程(包 ...
- select默认选中
- 笔记:Linux(AWS Redhat)开机启动workman进程(/etc/rc.local必须是755权限)
1.使用which命令查看php的安装路径 [root@ip---- ~]# which php /usr/bin/php 2.使用vim /etc/rc.local将要执行的命令加入/etc/rc. ...
- Python linux多版本共存以及虚拟环境管理(转摘)
Python linux多版本共存以及虚拟环境管理 2017年08月01日 18:42:25 sliderSun 阅读数:197更多 个人分类: python 版权声明:本文为博主原创文章,未经博 ...