题目:

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的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描list。

第一种方法,就是使用HashMap来坐,HashMap的key存原始pointer,value存新的pointer。

第一遍,先不copy random的值,只copy数值建立好新的链表。并把新旧pointer存在HashMap中。

第二遍,遍历旧表,复制random的值,因为第一遍已经把链表复制好了并且也存在HashMap里了,所以只需从HashMap中,把当前旧的node.random作为key值,得到新的value的值,并把其赋给新node.random就好。

代码如下:

 1 public RandomListNode copyRandomList(RandomListNode head) {
 2         if(head==null)
 3             return null;
 4         HashMap<RandomListNode,RandomListNode> map = new HashMap<RandomListNode,RandomListNode>();
 5         RandomListNode newhead = new RandomListNode(head.label);
 6         map.put(head,newhead);
 7         RandomListNode oldp = head.next;
 8         RandomListNode newp = newhead;
 9         while(oldp!=null){
             RandomListNode newnode = new RandomListNode(oldp.label);
             map.put(oldp,newnode);
             newp.next = newnode;
             
             oldp = oldp.next;
             newp = newp.next;
         }
         
         oldp = head;
         newp = newhead;
         while(oldp!=null){
             newp.random = map.get(oldp.random);
             oldp = oldp.next;
             newp = newp.next;
         }
         
         return newhead;
     }

上面那种方法遍历2次list,所以时间复杂度是O(2n)=O(n),然后使用了HashMap,所以空间复杂度是O(n)。

第二种方法不使用HashMap来做,使空间复杂度降为O(1),不过需要3次遍历list,时间复杂度为O(3n)=O(n)。

第一遍,对每个node进行复制,并插入其原始node的后面,新旧交替,变成重复链表。如:原始:1->2->3->null,复制后:1->1->2->2->3->3->null

第二遍,遍历每个旧node,把旧node的random的复制给新node的random,因为链表已经是新旧交替的。所以复制方法为:

node.next.random = node.random.next

前面是说旧node的next的random,就是新node的random,后面是旧node的random的next,正好是新node,是从旧random复制来的。

第三遍,则是把新旧两个表拆开,返回新的表即可。

代码如下:

 1  public RandomListNode copyRandomList(RandomListNode head) {
 2         if(head == null)  
 3             return head;  
 4         RandomListNode node = head;  
 5         while(node!=null){
 6             RandomListNode newNode = new RandomListNode(node.label);  
 7             newNode.next = node.next;  
 8             node.next = newNode;  
 9             node = newNode.next;  
         } 
         
         node = head;  
         while(node!=null){
             if(node.random != null)  
                 node.next.random = node.random.next;  
             node = node.next.next;  
         }
         
         RandomListNode newHead = head.next;  
         node = head;  
         while(node != null){  
             RandomListNode newNode = node.next;  
             node.next = newNode.next;  
             if(newNode.next!=null)  
                 newNode.next = newNode.next.next;  
             node = node.next;  
         }  
         return newHead;  
      }

Reference:http://blog.csdn.net/linhuanmars/article/details/22463599

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

  1. Copy List with Random Pointer [LeetCode]

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. [Leetcode Week17]Copy List with Random Pointer

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

  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. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

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

  6. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

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

  8. leetcode 138. Copy List with Random Pointer ----- java

    A linked list is given such that each node contains an additional random pointer which could point t ...

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

随机推荐

  1. python之路【第十二篇】: MYSQL

    一. 概述 Mysql是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用 ...

  2. tkinter-clock实例

    模仿着前辈的脚步,画了个临时的时钟显示: 代码如下: # coding:utf-8 from tkinter import * import math,time global List global ...

  3. 哪种写法更好?<script></script> vs/or <script type=”text/javasript”></script>

    一直很奇怪 哪种写法更好<script type=“text/javascript”>…</script> or <script>…</script>? ...

  4. [leetcode DP]91. Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. [leetcode shell]192. Word Frequency

    统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{prin ...

  6. Openstack-开发基础 stevedore学习

    在给openstack-N版加路由的时候发现怎么都无法搞定,原来现在用这个模块来处理了 stevedore是用来实现动态加载代码的开源模块.它是在OpenStack中用来加载插件的公共模块.可以独立于 ...

  7. python脚本 pyqt 打包成windows可执行exe文件 pyinstaller

    今天学习pyqt,做了一些好玩的东西. 好奇之中想试试python脚本编译成可执行文件,一顿查询之后成功了! 我的环境是: windows10 64bit    python3.5    pyqt5 ...

  8. [UOJ240]aliens

    学习了一下凸优化DP,感觉挺有意思的 首先把所有点对称到左下角,然后以每个点为顶点画等腰直角三角形,将被覆盖的点去掉,现在所有点从左上到右下横纵坐标都是递增的,设坐标为$(x_{1\cdots M}, ...

  9. ShellExecuteA函数

    原型: ShellExecuteA(, , , );//最大化打开记事本 第一个参数//系统启动第二个参数//open打开第三个参数//指令第四个参数//默认0第五个参数//默认0第六个参数//0隐藏 ...

  10. Java_JSP自定义标签的开发与应用

    在JSTL提供了四个标签库(核心标签库.国际化标签库.数据库标签库和XML标签库),涉及到了几十个标签.虽然这些标签可以完成比较复杂的工作,但它们仍然无法满足程序中的特殊需求.因此,就需要用户根据自己 ...