【题目描述】

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. hive reduce 阶段GC Exception

    某个reduce中的value堆积的对象过多,导致jvm频繁GC. 解决办法: 1. 增加reduce个数,set mapred.reduce.tasks=300,. 2. 在hive-site.xm ...

  2. elk-准备(一)

    一.在搭建elk之前需要做准备工作 1.创建elk用户 groupadd elk -g 1001 useradd elk -m -d /home/elk -s /bin/bash -g 1001 -u ...

  3. elementUI中table组件会出现空白部分

    先上图: 造成原因: width全部都写死了,(注释:不要全部都写死width,没写width的会自动分配宽度)  

  4. jquery截取地址栏中url参数的值

    <script> /*http://127.0.0.9/index.php?s=/Home/Index/fangguan_shuju&zc=2*/ function getQuer ...

  5. 我了解到的新知识之—MPLS

    下周末运营商来公司要对MPLS升级,对于一个多年权限管理经验的我来说未免有些陌生,幸好现在网络资源丰富,就开始了搜索之旅,找到了一些信息,所以在想干脆以后就开一个系列就叫<我了解到的新知识> ...

  6. Codeforces 677 - A/B/C/D/E - (Undone)

    链接: A - Vanya and Fence - [水] AC代码: #include<bits/stdc++.h> using namespace std; ; int n,h; in ...

  7. ExceptionLess使用

    1.os使用windows server 2012,省的升级iis express-iis 8(测试环境是IIS Express 8,生产环境IIS 7.5).PowerShell 3+了. 2.wi ...

  8. Python+Django 后台view异步接不到参数问题

    因为后台需获取前台的多个ID参数(checkbox) //获取checkbox的每个ID并放到数组内var _items = []; var items = document.getElementsB ...

  9. 编写Shell脚本的最佳实践

    编写Shell脚本的最佳实践 http://kb.cnblogs.com/page/574767/ 需要记住的 代码有注释 #!/bin/bash # Written by steven # Name ...

  10. vs添加github代码库

    1.安装git for windows 2.在vs中工具->扩展和更新,安装github extension 3.在项目中右键,添加源码到git,之后配置git,然后选择同步或者commit即可