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.

和第133题差不多,都是图的复制,区别在于这道题的label有可能是相同的,所以导致了map的key有可能相同,所以需要处理。

两种方法差不多。第二种更简洁。

1、在复制next之后修改原结构的label为顺序增长,方便建立map,之后再修改回来。

/**
* 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 map2 = new HashMap<Integer,RandomListNode>();
int num = 1; RandomListNode node = head;
RandomListNode newNode = new RandomListNode(head.label);
RandomListNode node2 = newNode;
map2.put(0,node2);
node.label = 0; node = node.next;
while( node != null ){
RandomListNode nextNode = new RandomListNode(node.label);
node2.next = nextNode;
node2 = node2.next;
node.label = num;
map2.put(num,node2);
num++;
node = node.next; } node = head;
node2 = newNode; while( node != null ){ if( node.random == null){
node = node.next;
node2 = node2.next;
continue;
} node2.random = (RandomListNode) map2.get( node.random.label ); node = node.next;
node2 = node2.next; }
node2 = newNode;
node = head;
while( node != null ){
node.label = node2.label;
node = node.next;
node2 = node2.next;
} return newNode; }
}

2、建立map的时候使用

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

leetcode 138. Copy List with Random Pointer ----- java的更多相关文章

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

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

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

  4. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  5. Leetcode#138 Copy List with Random Pointer

    原题地址 非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步: 1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"2. ...

  6. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

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

  8. [Leetcode Week17]Copy List with Random Pointer

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

  9. 【LeetCode】138. Copy List with Random Pointer

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

随机推荐

  1. RM报表预览,只有固定的1个订单页面

    明明选了多个记录,预览时,只显示最后一个. 原因: 主项数据的数据集选了报表自带的虚拟数据集了.

  2. 结对编程——关于Fault、Error、Failure程序设计

    一.问题描述:         构造程序,分别是:         •不能触发Fault         •触发Fault,但是不能触发Error         •触发Error,但是不能产生Fai ...

  3. Microsoft Mole原理及常见问题整理

     Moles与Moq(Rhino.Mocks)比较 作用范围 Moq与Rhino.Mocks这类的Mock是对Interface或AbstractClass做Mock, 而Moles是Mock整个 ...

  4. c++的调试与运行

    编译F9:运行F10:编译运行F11. 设置断点:在代码所在行的行首单击,该行即被加亮.注意:设置断点后,此时程序运行进入调试状态,要想运行程序,就不能使用F10或者F11,而是要使用F5调试,然后使 ...

  5. php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28

    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28 话说,嗯嗯,就是我自己说,做事认真要用表格,学习技 ...

  6. 关于binary search的一点解惑

    在写binary search时对于mid的计算我最开始使用的是 mid = (low + high)/2; 后来看到在很多的实现为 mid = low + (high - low)/2; 想了一下两 ...

  7. iOS提交AppStore被拒原因

    1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...

  8. Android布局居中的几种做法

    Android的布局文件中,如果想让一个组件(布局或View)居中显示在另一个布局(组件)中,可以由这么几种做法: android:layout_gravity android:gravity and ...

  9. 微信 关闭手机微信内置浏览器的js

    WeixinJSBridge.call('closeWindow');

  10. 重学STM32----(二)

    前几天买了个蓝牙模块,昨天到来了,就打算来研究研究蓝牙.看了蓝牙模块的资料,知道通讯需要串口,那肯定要先写一个串口程序了.要是用库函数写,10多分钟可能就会搞定,但是这就违背我的初衷了,所以就不知天高 ...