zoukankan      html  css  js  c++  java
  • 复制带随机指针的链表 · Copy List with Random Pointer

    [抄题]:

    给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。

    返回一个深拷贝的链表。 

    [思维问题]:

    [一句话思路]:

    完完全全地复制,否则不好操作。

    1->1`->2->2`->3->3`->4->4` 紧随其后地复制,再拆开

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. newNode是每次新建的节点,不用往后移。head每次要移动2格,才能复制。
    2. head = head.next.next意在往下,temp.next = temp.next.next;意在往下
    3. if中非空的条件都是指针:if (head.next.random != null)
    4. RandomListNode newHead = new RandomListNode(head.label);表示有角标的从属关系
    5. split用的是快慢指针的思想,temp head分别连接即可(用temp是为了写法更简单):

      while (head != null) {
      RandomListNode temp = head.next;暂存
      head = temp.next;连接head
      if (temp.next != null) {连接temp
      temp.next = temp.next.next;
      }
      }

    6. 主函数里要写corner caseif (head == null) { return null; }

    [二刷]:

    1. head.random.next是个数 = head.next.random是个指针;重点看最后一位
    2. splitList里面要用while循环,一直split
    3. head往后移动时,是head本身 = temp.next;
    4.  又不记得写corner case了,唉

    [总结]:

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [英文数据结构,为什么不用别的数据结构]:

    [其他解法]:

    哈希彪

    [Follow Up]:

    [题目变变变]:

    /**
     * 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 {
        /**
         * @param head: The head of linked list with a random pointer.
         * @return: A new head of a deep copy of the list.
         */
         private void copyNext(RandomListNode head) {
             while (head != null) {
                RandomListNode newHead = new RandomListNode(head.label);
                newHead.next = head.next;
                newHead.random = head.random;
                head.next = newHead;
                head = head.next.next;
             }
        }
        
        private void copyRandom(RandomListNode head) {
            while (head != null) {
                if (head.next.random != null) {
                    head.next.random = head.random.next;
                }
                head = head.next.next;
            }
        }
        
        private RandomListNode splitList(RandomListNode head) {
            RandomListNode newHead = head.next;
            while (head != null) {
                RandomListNode temp = head.next;
                head.next = temp.next;
                head = head.next;
                if (temp.next != null) {
                    temp.next = temp.next.next;
                }
            }
            return newHead;
        }
        
        public RandomListNode copyRandomList(RandomListNode head) {
            copyNext(head);
            copyRandom(head);
            return splitList(head);
        }
    }
    View Code
  • 相关阅读:
    Python 正则表达式(分组)
    django 笔记
    Java代理和动态代理机制分析和应用
    Chrome浏览器如何调试移动端网页信息
    【数据分析】Excle中安装数据分析工具
    【BigData】Java基础_socket编程中使用多线程
    【BigData】Java基础_多线程
    【BigData】Java基础_socket编程
    财务报表之利润表
    资产负债表的会计恒等式
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8159270.html
Copyright © 2011-2022 走看看