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. nbtstat -a <IP> 会显示主机名、所在工作组等信息

    nbtstat -a <IP> 会显示主机名.所在工作组等信息

  2. hdu1078 bfs

    //Accepted 468 KB 812 ms //bfs+dp #include <cstdio> #include <cstring> #include <iost ...

  3. Ubuntu 14.10 下设置静态IP

    修改 /etc/network/interfaces 文件 sudo nano /etc/network/interfaces 修改为 # 前面的不变auto eth0 iface eth0 inet ...

  4. (转)Ratchet教程:meta与link标签

    原文:http://www.w3cplus.com/mobile/meta-and-link-tags-for-ratchet.html Ratchet教程:meta与link标签           ...

  5. Android Context

    http://www.cnblogs.com/android100/p/Android-Context.html

  6. 利用strut2标签自动生成form前端验证代码

    利用strut2标签自动生成form前端验证代码,使用到的技术有1.struts2标签,如<s:form> <s:textfieled>2.struts2读取*Validati ...

  7. 极客DIY:RFID飞贼打造一款远距离渗透利器

    本文使用最新的渗透工具RFID飞贼(Tastic RFID Thief)和RFID感应破解技术来获取一些拥有安防的建筑物的访问权限. Tastic RFID Thief是一个无声远距离RFID读卡器, ...

  8. 进行以上Java编译的时候,出现unmappable character for encoding GBK。

    public class Exerc02{ public static void main(String args []){ char c = '中国人'; System.out.pingtln(c) ...

  9. ubuntu文件夹右键没有共享选项

    在配置samba的时候,不知道出了什么错误,我就删除了samba,之后在ubuntu文件上按右键就没有共享的选项了,这样每次配置都得进samba麻烦. 我重新安装了samba也不行,郁闷! 解决: 1 ...

  10. Day07_面向对象第二天

    1.构造方法(掌握) 1.构造方法的特点(掌握)      A.方法名必须和类名保持一致      B.没有返回值类型并且没有具体的返回值   2.构造方法的作用(掌握)       给对象的属性初始 ...