zoukankan      html  css  js  c++  java
  • LeetCode 138. Copy List with Random Pointer

    原题链接在这里:https://leetcode.com/problems/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.

    题解:

    每个node 带了一个random指针,问题就在于copy时也许这个random指针指向的还没有遍历到。

    第一种方法使用HashMap 做两遍itertion, 第一遍复制原有list包括next, 但是不复制random, 把original node 和 copy node 成对放到HashMap中,original node是键。第二遍遍历时把copy的random指向oringal node random 在HashMap中对应的点。

    如此Time Complexity: O(n). Space O(n), 这种方法就不写了。

    第二种方法不利用额外空间,那么只能利用原有数据结构。一共需要做三次iteration, 第一次遍历时对于每一个original node 做 copy 然后insert 到original node的next里。第二次遍历时如果origianl的random不指向null, 就让它后面的copy node random 指向原有random指向点的next, cur.next.random = cur.random.next, 因为random指向点的next 就是指向点的copy. 第三次遍历时把原有list 拆成两个list, 一个原有list, 一个复制list.

    Note: 做第二次遍历时要注意original random指向null的情况,因为后面用到了cur.random.next 若是指向null, 是没有next的.

    Time Complexity: O(n). Space O(1).

    AC Java:

     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){
    12             return null;
    13         }
    14         
    15         //First iteration, copy node and append it to the original node
    16         RandomListNode cur = head;
    17         while(cur != null){
    18             RandomListNode copy = new RandomListNode(cur.label);
    19             copy.next = cur.next;
    20             cur.next = copy;
    21             cur = cur.next.next;
    22         }
    23         
    24         //Second iteration, if original random doesn't point to null, assign corresponding copy node random  point to origianl 
    25         //random's next node
    26         cur = head;
    27         while(cur != null){
    28             if(cur.random != null){
    29                 cur.next.random = cur.random.next;
    30             }
    31             
    32             cur = cur.next.next;
    33         }
    34         
    35         //Third iteration, separate list into two. One is origianl list, the other is the copy list.
    36         cur = head;
    37         RandomListNode copyHead = cur.next;
    38         RandomListNode copyCur = copyHead;
    39         while(cur != null) {
    40             cur.next = cur.next.next;
    41             copyCur.next = copyCur.next != null ? copyCur.next.next : null;
    42             
    43             cur = cur.next;
    44             copyCur = copyCur.next;
    45         }
    46         
    47         return copyHead;
    48     }
    49 }

    DFS 可以像 Clone Graph.

    Time Complexity: O(N+E). Space: O(N).

     1 public class Solution {
     2     Map<Integer, RandomListNode> visited = new HashMap<Integer, RandomListNode>();
     3     
     4     public RandomListNode copyRandomList(RandomListNode head) {
     5         if(head == null){
     6             return head;
     7         }
     8         if(visited.containsKey(head.label)){
     9             return visited.get(head.label);
    10         }
    11         
    12         RandomListNode copy = new RandomListNode(head.label);
    13         visited.put(head.label, copy);
    14         
    15         copy.next = copyRandomList(head.next);
    16         copy.random = copyRandomList(head.random);
    17         return copy;
    18     }
    19 }
  • 相关阅读:
    Eclipse和MyEclipse常出现的错误总结
    JS中的showModelDialog详解和实例
    form中的action,submit="return save()"以及ajax提交方法
    清除上传文件input标签中type="file"的value值
    上传文件时并顺便将文件压缩时出现文件名乱码以及文件内容乱码解决方案
    python 知识
    centos 安装docker
    docker 配置国内镜像
    git 操作命令
    react build dist发布nginx时的配置
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4877096.html
Copyright © 2011-2022 走看看