【题目描述】

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 题解的更多相关文章

  1. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  2. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  3. 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 ...

  4. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  5. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  7. 【Lintcode】105.Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  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(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

随机推荐

  1. react表单的一些小例子

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. PHP(javascript基础)

    js浏览器的脚本语言js的基础语法和 . js Dom操作写法分类1.行内(内联)写在标签里面,以属性的形式表现,属性名是“事件属性名” 例如:<button onClick="js代 ...

  3. mongoDB数据库插入数据时报错:db.collection is not a function

    nodejs连接mongodb插入数据时,发现mongoDB报错:db.collection is not a function.解决方法: 1.npm下载mongodb2.x.x版本替换3.x.x ...

  4. Weex小笔记(自己理解,有错请指正)

    在Eros中,做的内容是封装了一些常用的框架,并且优化开发流程为将前端Vue文件打包出资源文件导入项目工程中(本地加载模式,需要注册文件.验证文件),然后原生移动端通过OC写module(功能模块类) ...

  5. 浅谈Java对象的equals方法

    相等与同一: 如果两个对象具有相同的类型以及相同的属性值,则称这两个对象相等. 如果两个引用对象指的是同一个对象,则称这两个变量同一. ==是一个比较运算符,基本数据类型比较的是值,引用数据类型比较的 ...

  6. Pycharm调试:进入调用函数后返回

    在菜单栏的view中勾选toolbar,然后点击工具栏中左箭头返回到调用函数处.

  7. android 知识汇总

    1.assets:不会在R.java文件下生成相应的标记,assets文件夹可以自己创建文件夹,必须使用AssetsManager类进行访问,存放到这里的资源在运行打包的时候都会打入程序安装包中, 2 ...

  8. 关于Java中StringBuffer的capacity问题

    从API查到capacity的作用是查看StringBuffer的容器容量是多少,刚开始纳闷这个跟length的区别在哪?试验了几次感觉有点不解.所以直接跟进源码分析. 直接通过new StringB ...

  9. 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码

    首发于公众号:计算机视觉life 旗下知识星球「从零开始学习SLAM」 这可能是最清晰讲解g2o代码框架的文章 理解图优化,一步步带你看懂g2o框架 小白:师兄师兄,最近我在看SLAM的优化算法,有种 ...

  10. Python基础(十二) 类私有成员和保护成员

    python中的protected和private python中用 _var :变量名前一个下划线来定义,此变量为保护成员protected,只有类及其子类可以访问.此变量不能通过from XXX ...