题目:

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. Java 中的数据类型

    我们学习Java就是为了编写程序完成功能,而什么是程序呢?程序 = 数据结构 + 算法. 分开看,数据结构指的是数据与数据之间的关系,那我们先来了解一下Java中的数据都是怎么表示的呢 ?也就是说数据 ...

  2. Hibernate与Mybatis对比

    Hibernate与Mybatis对比 两者相同点 Hibernate与MyBatis都可以是通过SessionFactoryBuider由XML配置文件生成SessionFactory,然后由Ses ...

  3. bzoj 5210(树链刨分下做个dp)

    5210: 最大连通子块和 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 211  Solved: 65[Submit][Status][Discus ...

  4. 在windows安装配置Git开发环境

    开始配置Git的开发环境.首先从google  code下载最新的windows的git安装包msysgit,当时我下载的是Git-1.7.4-preview20110204.exe,然后就开始安装了 ...

  5. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...

  6. Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造

    B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...

  7. tsinsen A1067. Fibonacci数列整除问题 dp

    A1067. Fibonacci数列整除问题 时间限制:1.0s   内存限制:512.0MB   总提交次数:2796   AC次数:496   平均分:51.83 将本题分享到:     查看未格 ...

  8. 解决firefox不支持innerText的办法

    js代码: <script> window.onload = function(){ if(window.navigator.userAgent.toLowerCase().indexOf ...

  9. Accessing an element's parent with ElementTree(转)

    Today I ran across a situation where I needed to programmatically remove specific elements from a KM ...

  10. Namespace declaration statement has to be the very first statement in the script

    php 中 Namespace declaration statement has to be the very first statement in the script 错误解决方法: 在PHP文 ...