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. Rhel6-haproxy+keepalived配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119:haproxy,keepalived server19.exa ...

  2. Spring学习笔记之方法注入

    public  abstract class ReplacedBean {protected static final Log log = LogFactory.getLog(ReplacedBean ...

  3. C# WinForm程序向datagridview里添加数据

    在C#开发的winform程序中,datagridview是一个经常使用到的控件.它可以以类似excel表格的形式规范的展示或操作数据,我也经常使用这个控件.使用这个控件首先要掌握的就是如何向其中插入 ...

  4. asp.net页面使用doPostBack的后台取值

    前台页面(aspx文件): --伪装按钮 <span onclick='__doPostBack("lkSend","key")'>发送</s ...

  5. Mahout0.9的安装与测试

    最近想实协同过滤的MR算法,但是网上查了一下,发现hadoop的生态系统中的Mahout的项目已经实现了相应的算法,因此想先尝试着实时这个mahout的使用及效果.要想用mahout必须要部署到had ...

  6. 《用C++语言编写一个程序,求PI的值》

    //编写一个C++程序求PI的值 /* PI=16arctan(1/5)-4arctan(1/239) 其中arctan用如下形式的极数计算: arctan=x-(x^3/3)+(x^5/7)-(x^ ...

  7. poj1141 区间dp+路径

    //Accepted 176 KB 47 ms //感谢大神们为我们这群渣渣铺平前进的道路!! //用scanf("%s",s)!=EOF WA到死 #include <cs ...

  8. ACM - KMP题目小结 (更新中)

    KMP算法题型大致有两类,一类是next数组的应用,一类是匹配问题. next数组大多数是求字符串周期,或者是与前缀后缀有关,也可以应用在DP中.需要对next数组有一定理解才能做得出. next数组 ...

  9. Pattern和Matcher

    java util本身提供了Pattern和Matcher(java.util.regex.Pattern,Matcher),两个类均是与正则表达式相关的类,其中: java.util.regex是一 ...

  10. SVG 2D入门8 - 文档结构

    前面介绍了很多的基本元素,包括结构相关的组合和重用元素,这里先对SVG的文档结构中剩下的相关元素简单总结一下,然后继续向前领略SVG的其他特性. SVG文档的元素基本可以分为以下几类: 动画元素:an ...