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. jQuery 中的 Ajax

    jQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...

  2. Spring与Hibernate整合,实现Hibernate事务管理

    1.所需的jar包 连接池/数据库驱动包 Hibernate相关jar Spring 核心包(5个) Spring aop 包(4个) spring-orm-3.2.5.RELEASE.jar     ...

  3. 第二周 PSP项目计划

    项目计划总结: 日期|任务 听课 编写程序 阅读相关书籍 网上查找资料   日总计 周一 2 1 2   5 周二     1    1 周三     2     2 周四 2 2     4 周五 ...

  4. wsgi协议

    用来为server程序和app/framework程序做连接桥梁的,使server和app/framework各自发展,任意组合 上图是python3.4标准库里面,关于wsgiserver的实现.从 ...

  5. [转]Putty中文乱码解决方法

    from: http://www.putty.ws/putty-luanma putty使用的过程中,你是否遇到过putty出现中文乱码的情况呢?putty终端出现乱码,是情况,多数是由于系统或软件缺 ...

  6. Linux下备份系统至另一硬盘

    首先会想到dd命令. 但,, 1,若是小硬盘还好,上T的大硬盘这样做肯定不明智; 2,况且dd是在硬件层面的拷贝,前面的MBR也会随之恢复到另一个盘,若源硬盘是100G,目标盘是200G,又会出问题, ...

  7. ios页面间传递参数四种方式

    ios页面间传递参数四种方式 1.使用SharedApplication,定义一个变量来传递. 2.使用文件,或者NSUserdefault来传递 3.通过一个单例的class来传递 4.通过Dele ...

  8. 使用开源工具MonoDevelop开发GTK#图形界面

    转自:http://developer.51cto.com/art/201011/235040.htm Mono一直到现在的2.8已经完全可以胜任一些比较小的项目了,但相关的开发文档与教程一直比较匮乏 ...

  9. Ubuntu 14.10 下Ganglia监控Hadoop集群

    前提是已经安装好Ganglia和Hadoop集群 1 Master节点配置hadoop-metrics2.properties # syntax: [prefix].[source|sink|jmx] ...

  10. Python的文件类型

    Python的文件类型主要分为3种:源代码(source file).字节码(byte-code file).优化的字节码(optimized file).这些代码都可以直接运行,不需要编译或者连接. ...