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. python 元祖(tuple)

    元祖和列表几乎相同,但是元祖一旦初始化是不可变更内容的 元祖的表示方式是caassmates=(), 要记住所有列表能用的.元祖都能用,但是就是不能变内容 注:记住,在python中的元祖,为了引起不 ...

  2. Git 常用命令详解(二)

    Git 是一个很强大的分布式版本管理工具,它不但适用于管理大型开源软件的源代码(如:linux kernel),管理私人的文档和源代码也有很多优势(如:wsi-lgame-pro) Git 的更多介绍 ...

  3. 在VS2012中编译WinXP兼容的程序

    VS2012默认是不兼容Windows XP的,编译链接出来的程序只能在Windows Vista及以上版本的操作系统上运行.可是有时需要在Windows XP上运行,又不得不用VS2012(例如用了 ...

  4. NOI题库 09:图像旋转翻转变换

    NOI题库开始的题,也是略水,当然也是大水,所以彼此彼此 09:图像旋转翻转变换 总时间限制: 1000ms 内存限制: 65536kB 描述 给定m行n列的图像各像素点灰度值,对其依次进行一系列操作 ...

  5. hdu 1205 吃糖果

    思路: 仔细想想,想要不重复吃一种糖果, 把所有糖果吃完,只要所有糖果的和,减去最多的糖果+1>=最多糖果的数量即可不重复吃完. #include <stdio.h> int mai ...

  6. Oracle 常用入侵命令

    1.查看当前数据库实例名称:select * from v$instance;2.查看当前用户的角色:select * from user_role_privs;3.查看当前用户下所有的表:selec ...

  7. IIS6.0文件解析漏洞小结

    今天搞站,本来这个站是aspx的,webserver是IIS6.0的,进入后台之后,发现有一个上传图片的地方,于是,我就上传了一张asp/aspx的一句话图片木马,但是用菜刀连接的时候,没有成功get ...

  8. B/S C/S架构的界面测试

    网站是B/S架构的典型,从做网站的有限经验来整理一下B/S测试的基本要点,并把它与C/S进行区分. 与C/S相比,以下4个测试是除了常用测试外还要注意的: (1)链接测试 (2)表单测试 (3)脚本测 ...

  9. C/C++中的getline函数总结:

    来自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html C/C++中的getline函数总结 getline函数是一个比较 ...

  10. 和我一起来了解SEO

    基础知识 搜索引擎 搜索引擎爬虫会检索各个网站,分析他们的关键字,从一个连接到另一个连接,如果爬虫觉得这个关键字是有用的 就会存入搜索引擎数据库,反之如果没用的.恶意的.或者已经在数据库的,就会舍弃. ...