Day65 复制带随机指针的链表

作者: Shimmer_ | 来源:发表于2021-04-02 23:50 被阅读0次

    给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。构造这个链表的 深拷贝。

    深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。

    https://leetcode-cn.com/problems/copy-list-with-random-pointer/

    示例1:

    输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
    输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

    image

    示例2:

    输入:head = [[1,1],[2,1]]
    输出:[[1,1],[2,1]]

    image

    示例3:

    输入:head = [[3,null],[3,0],[3,null]]
    输出:[[3,null],[3,0],[3,null]]

    image

    示例4:

    输入:head = []
    输出:[]
    解释:给定的链表为空(空指针),因此返回 null。

    提示:

    0 <= n <= 1000
    -10000 <= Node.val <= 10000
    Node.random 为空(null)或指向链表中的节点。

    Java解法

    思路:

    • 曾经做过这题,大致思路是,对链表进行复制,并将复制过的节点使用hashmap存放,避免多次重建,但条件不一致,随机指针表示的是位置,但这个题目的指针是指向引用,导致hashmap不太好用了
    • 改用笨方法,记录拷贝
    package sj.shimmer.algorithm.m4_2021;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by SJ on 2021/4/2.
     */
    
    class D65 {
        public static void main(String[] args) {
    //        [[7,null],[13,0],[11,4],[10,2],[1,0]]
            Node head1 = new Node(7);
            Node head2 = new Node(13);
            Node head3 = new Node(11);
            Node head4 = new Node(10);
            Node head5 = new Node(1);
            head1.next = head2;
            head1.random = null;
            head2.next = head3;
            head2.random = head1;
            head3.next = head4;
            head3.random = head4;
            head4.next = head5;
            head4.random = head2;
            head5.next = null;
            head5.random = head1;
            System.out.println(copyRandomList(head1));
        }
    
        public static Node copyRandomList(Node head) {
            List<Node> list = new ArrayList<>();
            List<Node> nlist = new ArrayList<>();
            while (head != null) {
                Node temp = head;
                list.add(temp);
                Node node = new Node(head.val);
                head = head.next;
                nlist.add(node);
            }
            int size = list.size();
            for (int i = 0; i < size; i++) {
                Node random = list.get(i).random;
                if (random == null) {
                    nlist.get(i).random = null;
                }else {
                    nlist.get(i).random = nlist.get(list.indexOf(random));
                }
                if (i<size-1){
                    nlist.get(i).next=nlist.get(i+1);
                }
            }
            return size==0?null:nlist.get(0);
        }
    
        static class Node {
            int val;
            Node next;
            Node random;
    
            public Node(int val) {
                this.val = val;
                this.next = null;
                this.random = null;
            }
        }
    }
    
    image

    官方解

    https://leetcode-cn.com/problems/copy-list-with-random-pointer/solution/fu-zhi-dai-sui-ji-zhi-zhen-de-lian-biao-by-leetcod/

    1. 回溯

      利用hashmap记录已访问的点,考虑的少了,不然可以用这种解法处理

      • 时间复杂度:O(N)
      • 空间复杂度:O(N)
    2. O(N) 空间的迭代

      只需要为 random 指针和 next 指针指向的未访问过节点创造新的节点并赋值即可,详见链接解析

    3. O(1) 空间的迭代

      牛比处理:

      通过扭曲原来的链表,并将每个拷贝节点都放在原来对应节点的旁边。这种旧节点和新节点交错的方法让我们可以在不需要额外空间的情况下解决这个问题

    相关文章

      网友评论

        本文标题:Day65 复制带随机指针的链表

        本文链接:https://www.haomeiwen.com/subject/nisckltx.html