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 判断js,css是否引入,确保不重复引入
基本原理:function loadjscssfile(filename, filetype){if (filetype=="js"){ //if filename is a ...
- Web前端开发推荐书籍
Web前端开发推荐书籍 前言 学校里没有前端的课程,那如何学习JavaScript,又如何使自己成为一个合格的前端工程师呢? 读 书吧~相对于在网上学习,在项目中学习和跟着有经验的同事学习,书中有着相 ...
- Ubuntu 14.04循环登录问题(密码正确,无法登录)
今天遇到了ubuntu 的循环登录问题,即使输入的密码是正确的,也无法成功登录.查询资料,发现有各种问题引起的循环登录问题,比如安装了jdk,或者安装了驱动. 最后找到了问题的原由:我的电脑是双显卡, ...
- 解决键盘输入被JDB占用的问题
解决键盘输入被JDB占用的问题 本周的任务"迭代和JDB"在使用JDB调试时需要键盘输入数据,但我在正确的位置输入数据后发现JDB提示如图所示的错误. 上网查找后得知该错误的产生是 ...
- linux安装jdk mysql
mysql 安装进入到mysql官网 点击archives 选择版本下载tar linux 安装mysql数据库——tar.gz包解压安装法 mysql数据库有多种安装方式,本文只介绍在Linux服务 ...
- en-zh(社会问题)social problems-2
让屏幕代替父母陪孩子?世卫组织:这样是不对的! No sedentary screen time for babies, WHO says Babies and toddlers should not ...
- 目标检测(三)Fast R-CNN
作者:Ross Girshick 该论文提出的目标检测算法Fast Region-based Convolutional Network(Fast R-CNN)能够single-stage训练,并且可 ...
- iptables 分析(三)
原文:http://blog.chinaunix.net/uid-24207747-id-2622902.html find_target查到目标并加载成功,返回一个xtables_target型对象 ...
- python连接mysql-PyMySql模块
安装 pip3 install pymysql 使用 输出mysql版本 import pymysql # 打开数据库连接 db = pymysql.connect("localhost&q ...
- VUE—打印(原生态网页打印)
//打印触发的方法 print(e){ let subOutputRankPrin = document.getElementById('printcode'); var options = { fo ...