【LeetCode】Copy List with Random Pointer
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.
思路:第一遍正常复制链表,同一时候用哈希表保存链表中原始节点和新节点的相应关系,第二遍遍历链表的时候,再复制随机域。
这是一种典型的空间换时间的做法,n个节点,须要大小为O(n)的哈希表,同一时候时间复杂度能够减少到O(n)。
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return head;
}
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode res = null;
RandomListNode taiListNode = null;
RandomListNode cur = head;
while (cur != null) {
if (res == null) {
res = new RandomListNode(cur.label);
res.next = res.random = null;
taiListNode = res;
map.put(head, res);
}
else{
taiListNode.next = new RandomListNode(cur.label);
taiListNode = taiListNode.next;
map.put(cur, taiListNode); }
cur = cur.next;
}
taiListNode.next = null;
cur = head;
taiListNode = res;
while (cur != null) {
taiListNode.random = (RandomListNode)map.get((RandomListNode)cur.random);
cur = cur.next;
taiListNode = taiListNode.next;
}
return res;
}
}
【LeetCode】Copy List with Random Pointer的更多相关文章
- 【leetcode】Copy List with Random Pointer (hard)
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【Leetcode】【Hard】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
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- Java for LeetCode 138 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 138. Copy List with Random Pointer ----- java
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- Ubuntu 11.04 安装后要做的20件事情
转自:http://www.cnbeta.com/articles/141137.htm #1 不喜欢Unity? 切换到Ubuntu gnome 经典桌面 注销unity桌面环境,然后选择登录环境为 ...
- ICP备案的一些tips~
1)一个单位是一个备案主体,只能有一个主体备案号: 2)一个企业下面可以有多个网站,依次在备案号后加-1 -2,以此类推,也叫网站备案号,每个网站只能有一个网站备案号.(所谓网站,不是指域名,也不是i ...
- JDBC数据库编程:ResultSet接口
掌握ResultSet接口 使用ResultSet接口进行查询 ResultSet接口 在JDBC操作中,数据库所有查询记录将使用ResultSet进行接收,并使用ResultSet显示内容. 常用方 ...
- 基于Qt的A*算法可视化分析
代码地址如下:http://www.demodashi.com/demo/13677.html 需求 之前做过一个无人车需要自主寻找最佳路径,所以研究了相关的寻路算法,最终选择A算法,因为其简单易懂, ...
- 分享一下自己ios开发笔记
// ********************** 推断数组元素是否为空 ********************** NSString *element = [array objectAtIndex ...
- 给第三方dll加上强命名的方法[C#]
在VS.NET 的命名行窗口下,输入如下的代码. 1 ,生成一个KeyFile sn -k keyPair.snk 2, 得到程序集的MSIL ildasm SomeAssembly.dll /out ...
- 严重: Exception starting filter struts2 Unable to load configuration. - [unknown location]
一般来说,按照这个流程下来是没有错的:SSH三大框架合辑的搭建步骤 但是,近来的一个测试例子出现了以下这个问题,困扰了许久!! 各种百度&各种问同学,最后请教了张老师后问题得到解决: 1.这种 ...
- 系统流畅/性能受限 谷歌Nexus4详细评测
http://mobile.it168.com/a2012/1220/1437/000001437938_8.shtml
- linux磁盘满时,如何定位并删除文件
原文链接: http://www.cnblogs.com/yinxiangpei/articles/4211743.html @1.一般情况 一般情况下先df看一下,然后cd到要满的盘,执行: d ...
- asp.net 查询本地excel 获取信息
string filepath = @"D:\test1.xls"; string sheetname = "Sheet5"; ...