【题目描述】

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. 参看gitlab版本号

    cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

  2. 解决vue单页路由跳转后scrollTop的问题

    作为vue的初级使用者,在开发过程中遇到的坑太多了.在看页面的时候发现了页面滚动的问题,当一个页面滚动了,点击页面上的路由调到下一个页面时,跳转后的页面也是滚动的,滚动条并不是在页面的顶部 在我们写路 ...

  3. day19:常用模块(collections,time,random,os,sys)

    1,正则复习,re.S,这个在用的最多,re.M多行模式,这个主要改变^和$的行为,每一行都是新串开头,每个回车都是结尾.re.L 在Windows和linux里面对一些特殊字符有不一样的识别,re. ...

  4. c语言实现wc功能

    本随笔对网站http://blog.chinaunix.net/uid-22566367-id-381958.html有所借鉴 #include <stdio.h> #define BEG ...

  5. SQL[Err]ORA-00932: inconsistent datatypes: expected NUMBER got CHAR:

    ORA-00932: inconsistent datatypes: expected NUMBER got CHAR: 获取的目标类型与源类型不一致,多出现在case when 语句中,when的结 ...

  6. 【Python全栈-JavaScript】JavaScript的window.onload()与jQuery 的ready()的区别

    JavaScript的window.onload()与jQuery 的ready()的区别 做web开发时常用Jquery中$(document).ready()和JavaScript中的window ...

  7. Vue2.0 v-for 中 :key 到底有什么用?

    要解释 key 的作用,不得不先介绍一下虚拟 DOM 的 Diff 算法了. vue 和 react 的虚拟 DOM 的Diff算法大致相同,其核心是基于两个简单的假设: 1.两个相同的组件产生类似的 ...

  8. Redis入门到高可用(十九)——Redis Sentinel

    一.Redis  Sentinel架构     二.redis sentinel安装与配置 四.客户端连接Sentinel            四.实现原理—— 故障转移演练(客户端高可用) 五.实 ...

  9. mysql如何给字母数字混合的字段排序?

    mysql> select * from t_SpiritInside; +------+ | col  | +------+ | s1   | | s2   | | s11  | | s12  ...

  10. Viewer.js插件浏览图片

    https://www.jianshu.com/p/e3350aa1b0d0 Viewer.js插件浏览图片 Viewer.js插件浏览图片 Viewer.js插件浏览图片