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.
分析:
这题的关键其实是如何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的更多相关文章
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 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 ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- [LeetCode] 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(带random引用的单链表深拷贝)
问题: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode 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 (hard)
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- ajax使用post提交中文
Ajax使用POST提交中文乱码问题 前段时间写JSP,使用AJAX以POST方式提交数据,如果是中文字符提交就会乱码,后来写ASP时用到AJAX以POST方式提交数据,中文一样是乱码.搜索一下相关资 ...
- COLORBOX文档
1,flash覆盖colorbox: 2,colorbox在ie中的位置和行为异常: 3,colorbox的位置和行为异常(不区分浏览器): 4,用colorbox显示外部文档时显示不正确: 5,在i ...
- 【Aizu 2305】Beautiful Currency
题 题意 给你n个货币价格,然后通过调整一些货币的大小,使得所有比自己小的货币都是该货币的约数,调整前第 i 货币为a,调整后为b 那么变化率为 ri=|a-b|/a ,总变化率为max(ri).求最 ...
- 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...
- Oracle新建数据库(新用户)
1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...
- 洛谷P2014 TYVJ1051 选课
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- TYVJ1864 守卫者的挑战
P1864 [Poetize I]守卫者的挑战 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜 ...
- unix/linux进程详解
技术分享 启动新进程 stdlib.hintsystem(const char *string)whichequals to "sh -c string" 替换进程映像unistd ...
- JS 在线网站
JS 在线压缩网站 国内压缩js网站http://tool.css-js.com/ uglifyjs 压缩js网站http://lisperator.net/uglifyjs JS在线格式化网站 ht ...
- 《驾驭Core Data》 第三章 数据建模
本文由海水的味道编译整理,请勿转载,请勿用于商业用途. 当前版本号:0.1.2 第三章数据建模 Core Data栈配置好之后,接下来的工作就是设计对象图,在Core Data框架中,对象图被表 ...