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. OC基础--OC内存管理原则和简单实例

    ARC: 由于自己的学习视频太早,Xcode是iOS6版本,新建命令行项目后,系统会默认启动ARC机制,全程Automatic Reference Counting,简单的说,就是代码中自动加入了re ...

  2. 【HDU 2604】Queuing

    题 题意 f和m两种字母组成字符串,fmf 和 fff 这种为不安全的字符串,现在有2*L个字母,问你有多少安全的字符串.答案mod M. 分析 递推,这题本意是要用矩阵快速幂.不过我发现这题好神奇, ...

  3. yii授权

    ACF (访问控制过滤器) 在你控制器的添加下列的 行为 方法 use yii\filters\AccessControl; class DefaultController extends Contr ...

  4. 人工鱼群算法-python实现

    AFSIndividual.py import numpy as np import ObjFunction import copy class AFSIndividual: "" ...

  5. zoj3819Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  6. RONOJ 6今明的预算方案(有依赖的背包)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  7. knockoutjs + easyui.treegrid 可编辑的自定义绑定插件

    http://blog.csdn.net/maddemon/article/details/16846183 目前仅支持URL的CRUD.不需要的话可以却掉相关代码,把treegrid的data直接赋 ...

  8. MVC 返回图片

    //调用 http://localhost:60663/home/GetCoder39Img?mycode=123443545 public void GetCoder39Img(string myc ...

  9. KVM切换声音关闭

    Scroll Lock 2次+左右键 实现切换 Scroll Lock 2次+"B" 实现声音的开关

  10. find只查当前目录 和 -exec和xargs区别

    1.find默认查找当前目录和子目录,通过maxdepth限制只查当前目录: find . -maxdepth 1 -type f -name "*.php" 2. find . ...