zoukankan      html  css  js  c++  java
  • Leetcode: Copy List with Random Pointer

    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.

    难度:77. 第一种思路是先按照复制一个正常链表的方式复制,复制的时候把复制的结点做一个HashMap,以旧结点为key,新节点为value。这么做的目的是为了第二遍扫描的时候我们按照这个哈希表把结点的随机指针接上。总共要进行两次扫描,所以时间复杂度是O(2*n)=O(n)。空间上需要一个哈希表来做结点的映射,所以空间复杂度也是O(n)。

     1 /**
     2  * Definition for singly-linked list with a random pointer.
     3  * class RandomListNode {
     4  *     int label;
     5  *     RandomListNode next, random;
     6  *     RandomListNode(int x) { this.label = x; }
     7  * };
     8  */
     9 public class Solution {
    10     public RandomListNode copyRandomList(RandomListNode head) {
    11         if (head == null) return null;
    12         RandomListNode dummy = new RandomListNode(0);
    13         RandomListNode scan = dummy;
    14         RandomListNode cur = head;
    15         HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
    16         while (cur != null) {
    17             scan.next = new RandomListNode(cur.label);
    18             map.put(cur, scan.next);
    19             cur = cur.next;
    20             scan = scan.next;
    21         }
    22         cur = head;
    23         scan = dummy;
    24         while (cur != null) {
    25             scan.next.random = map.get(cur.random);
    26             cur = cur.next;
    27             scan = scan.next;
    28         }
    29         return dummy.next;
    30     }
    31 }

    第二遍做的时候的做法:

     1 public class Solution {
     2     public RandomListNode copyRandomList(RandomListNode head) {
     3         if (head == null) return null;
     4         HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
     5         RandomListNode current = head;
     6         while (current != null) {
     7             RandomListNode copy = new RandomListNode(current.label);
     8             map.put(current, copy);
     9             current = current.next;
    10         }
    11         current = head;
    12         while (current != null) {
    13             map.get(current).next = map.get(current.next);
    14             map.get(current).random = map.get(current.random);
    15             current = current.next;
    16         }
    17         return map.get(head);
    18     }
    19 }

    这个方法毕竟要需要哈希表来存,占用额外空间,还不算最优,因此考虑能不能有不需要额外空间的做法。参考网上的思路,发现想避免使用额外空间,我们只能通过利用链表原来的数据结构来存储结点。基本思路是这样的,对链表进行三次扫描,第一次扫描对每个结点进行复制,然后把复制出来的新节点插入在原结点的next,也就是让链表变成一个重复链表,就是新旧更替;第二次扫描中我们把旧结点的随机指针赋给新节点的随机指针,因为新结点都跟在旧结点的下一个,所以赋值比较简单,就是node.next.random = node.random.next,其中node.next就是新结点,因为第一次扫描我们就是把新结点接在旧结点后面。现在我们把结点的随机指针都接好了,最后一次扫描我们把链表拆成两个,第一个还原原链表,而第二个就是我们要求的复制链表。因为现在链表是旧新更替,只要把每隔两个结点分别相连,对链表进行分割即可。这个方法总共进行三次线性扫描,所以时间复杂度是O(n)。而这里并不需要额外空间,所以空间复杂度是O(1)。比起上面的方法,这里多做一次线性扫描,但是不需要额外空间,还是比较值的。这个做法难度:95

     1 public RandomListNode copyRandomList(RandomListNode head) {
     2     if(head == null)
     3         return head;
     4     RandomListNode node = head;
     5     while(node!=null)
     6     {
     7         RandomListNode newNode = new RandomListNode(node.label);
     8         newNode.next = node.next;
     9         node.next = newNode;
    10         node = newNode.next;
    11     }
    12     node = head;
    13     while(node!=null)
    14     {
    15         if(node.random != null)
    16             node.next.random = node.random.next;
    17         node = node.next.next;
    18     }
    19     RandomListNode newHead = head.next;
    20     node = head;
    21     while(node != null)
    22     {
    23         RandomListNode newNode = node.next;
    24         node.next = newNode.next;
    25         if(newNode.next!=null)
    26             newNode.next = newNode.next.next;
    27         node = node.next;
    28     }
    29     return newHead;
    30 }
  • 相关阅读:
    Oracle EBS-SQL (MRP-7):检查MRP计划运行报错原因之超大数据查询2.sql
    Oracle EBS-SQL (MRP-6):检查MRP计划运行报错原因之超大数据查询1.sql
    Oracle EBS-SQL (MRP-5):重起MRP Manager.sql
    Oracle EBS-SQL (INV-12):检查待定事物处理1.sql
    Oracle EBS-SQL (OM-6):打开订单.sql
    Oracle EBS-SQL (INV-11):检查子库存会计信息.sql
    Oracle EBS-SQL (OM-5):检查订单事务处理类型.sql
    Oracle EBS-SQL (OM-4):检查发运网络.sql
    Oracle EBS-SQL (PO-18):检查工作台下达的PR在系统找不到.sql
    Oracle EBS-SQL (PO-17):检查供货比例不为100%.sql
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/3978436.html
Copyright © 2011-2022 走看看