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.

分析:

这题的关键其实是如何copy random pointer,找到一个random pointer指向的node, time complexity is O(n). 所以总的复杂度在O(n^2).

这里有一个比较巧妙的方法。在每个Node后面添加一个Node,新node的值和前一个Node一样。这样的话random pointer就很容易copy了,

 /**
* 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 {
/**
* @param head: The head of linked list with a random pointer.
* @return: A new head of a deep copy of the list.
*/
public RandomListNode copyRandomList(RandomListNode listHead) {
if (listHead == null) return listHead;
RandomListNode current = listHead;
// add duplicate nodes
while(current != null) {
RandomListNode node = new RandomListNode(current.label);
node.next = current.next;
current.next = node;
current = current.next.next;
} // set random pointer
current = listHead;
while (current != null) {
if (current.random != null) {
current.next.random = current.random.next;
}
current = current.next.next;
} // restore and detach
RandomListNode newHead = listHead.next;
RandomListNode current1 = listHead;
RandomListNode current2 = newHead;
while (current1 != null) {
current1.next = current2.next;
current1 = current1.next;
41 if (current1 != null) { // very important, cannot remove it.
42 current2.next = current1.next;
43 }
current2 = current2.next;
}
return newHead;
}
}

另一种更好的方法:利用hashmap保存original和copy

 /**
* 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 null; Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode current = head;
while(current != null) {
map.put(current, new RandomListNode(current.label));
current = current.next;
} RandomListNode newHead = map.get(head); while(head != null) {
RandomListNode temp = map.get(head);
temp.next = map.get(head.next);
temp.random = map.get(head.random);
head = head.next;
}
return newHead;
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Copy List with Random Pointer的更多相关文章

  1. 16. Copy List with Random Pointer

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

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

  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. Copy List with Random Pointer leetcode java

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

  5. LintCode - Copy List with Random Pointer

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

  6. [Leetcode Week17]Copy List with Random Pointer

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

  7. [LeetCode] 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——Copy List with Random Pointer(带random引用的单链表深拷贝)

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

  9. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

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

随机推荐

  1. JS模式:Mixin混合模式,=_=!就是常见的Object.create()或者_extend()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 曲线行驶s弯道技巧图解【转】

    s弯道怎么走?在走S弯的时候,最主要的就是控制车的速度,在做每个动作的时候要保持一样的速度,不要一会快一会慢的,在开的时候,因为每个人的身高,体型不一样,每个人看的点位都是不一样的,每次在开的时候要找 ...

  3. 【转】getHibernateTemplate出现的所有find方法的总结

    一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...

  4. 基于SVD的推荐算法

    首先每行减去每列的均值,然后svd分解,得到USV,然后US代表用户矩阵u,SV代表项目矩阵v,那么预测评分为用户均值加上uv. 降维方法扩展性好,不过降维导致信息损失,而且与数据及相关,高维情况下效 ...

  5. 让Jayrock插上翅膀(加入输入输出参数注释,测试页面有注释,下拉框可以搜索)

    继上一篇文章介绍了Jayrock组件开发接口的具体步骤和优缺点之后,今天给大家带来的就是,如何修复这些缺点. 首先来回顾一下修复的缺点有哪些: 1.每个接口的只能写大概的注释,不能分开来写,如接口的主 ...

  6. 轻量级应用开发之(06)Autolayout自动布局1

    一 什么是Autolayout Autolayout是一种“自动布局”技术,专门用来布局UI界面的. 自IOS7 (Xcode 5)开始,Autolayout的开发效率得到很大的提高. 苹果官方也推荐 ...

  7. Java Observer 观察者

    http://www.cnblogs.com/jaward/p/3277619.html 1.API 被观察者 java.util.Observable; public class Observabl ...

  8. sourceinsight安装记录

    sourceinsight安装记录 此文章为本人使用sourceinsight一个星期之后的相关设置步骤记录和经验记录,以备以后查验,网上的相关资料都也较为完善,但是对于新手还是有一定困难的,所以在这 ...

  9. 2层Xml读取类

    配置文件 <?xml> <root> <parent name="C"> <child name="C1">Sp ...

  10. Java及Android开发环境搭建

    前言 自从接触java以来,配置环境变量折腾了好几次,也几次被搞得晕头转向,后来常常是上网查阅相关资料才解决.但是过一段时间后一些细节就会记不清了,当要在其他机子上配置时又得上网查或者查阅相关书籍,如 ...