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

  1. 【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 ...

  2. 【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 ...

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

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

  4. [Leetcode Week17]Copy List with Random Pointer

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

  5. 【LEETCODE OJ】Copy List with Random Pointer

    Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...

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

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

  8. [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 ...

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

随机推荐

  1. canvas锯齿

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 学会使用简单的MySQL操作

    第十八章 学会使用简单的MySQL操作 在前面两个章节中已经介绍过MySQL的安装了.可是光会安装还不够.还须要会一些主要的相关操作.当然了,关于MySQL的内容也是非常多的.仅仅只是对于linux系 ...

  3. hp-ux 集群,内存 小记

    -----查看hp 集群状态信息 # cmviewcl -v CLUSTER        STATUS       dbsvr          up               NODE      ...

  4. 使用Nexus创建私服

    原文链接:http://www.cnblogs.com/helong/articles/2254446.html 注意:nexus2.6.0版本以后不支持jdk6了,必须jdk1.7或以上. 参考:h ...

  5. gitlab8.0 一键安装 经过自己测试 发送邮件部分最难搞 国内没有说明白的

    邮件发送部分,弄了一天终于弄好啦,FQ过去查的资料,奶奶的无语 Gitlab搭建步骤 一:操作系统环境 CentOS: 6.5 –x86-64 二:安装方式 一种是自定义安装,一种是一键安装 三:自定 ...

  6. iOS 键盘自适应(IQKeyboardManager)使用小结

    IQKeyboardManager Github地址 经常在开发一个应用程序,我们遇到了一个问题,iPhone的键盘上滑覆盖的UITextField / UITextView.IQKeyboardMa ...

  7. MapReduce编程(七) 倒排索引构建

    一.倒排索引简单介绍 倒排索引(英语:Inverted index),也常被称为反向索引.置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射. ...

  8. dbutils使用---QueryRunner、BeanListHandler、BeanHandler、MapListHandler、MapHandler、ScalarHandler

    1. ResultSetHandler 的作用: QueryRunner 的 query 方法的返回值最终取决于 query 方法的 ResultHandler 参数的 hanlde 方法的返回值. ...

  9. Errors occurred while updating the change sets for SVNStatusSubscriber org.apache.subversion.javahl.

    原因:eclipse-svn插件版本过低,导致不能识别svn客户端中的代码,从而报错 解决方法: 1.点击“Help”下拉菜单中的“Eclipse Marketplace”, 2.在弹出的窗口中,点击 ...

  10. ant.xml

    <?xml version="1.0"?> <project name="dxcc" default="buildplugins&q ...