IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 我要进大厂-算法-第五天 -> 正文阅读

[数据结构与算法]我要进大厂-算法-第五天

阅读该文章预计30分钟

一:复制含有随机指针节点的链表

1.1:题目描述

【题目】 一种特殊的链表节点类描述如下:

    public static class Node {
        private int value;
        private Node next;
        private Node rand;
        public Node(int value) {
            this.value = value;
        }
    }

Node类中的value是节点值,next指针和正常单链表中next指针的意义
一 样,都指向下一个节点,rand指针是Node类中新增的指针,这个指
针可 能指向链表中的任意一个节点,也可能指向null。 给定一个由
Node节点类型组成的无环单链表的头节点head,请实现一个 函数完成
这个链表中所有结构的复制,并返回复制的新链表的头节点。

进阶:
不使用额外的数据结构,只用有限几个变量,且在时间复杂度为 O(N)
内完成原问题要实现的函数。

1.2:思路分析

1.2.1:空间复杂度O(N)
使用map把所有结点储存起来,只保存他的值,不保存他的引用

然后遍历头节点
给每个节点的引用(next、rand)指针赋值
复制的节点从map中拿出,组装next、rand指针,因为保存的是他的引用,所以用map刚好
1.2.2:空间复杂度O(1)
将所有节点都复制一份,复制到他的下面
如这样 A-B-C  复制后:A-A-B-B-C-C

然后再给第二个A、B、C复制next、rand指针
next指针就是原来next指针的下一个
rand指针就是原来rand指针的下一个

1.3:Java代码

package xyz.fudongyang.basic.class_03.my;

import java.util.HashMap;
import java.util.Map;

public class Code_13_CopyListWithRandom {

    public static class Node {

        private int value;
        private Node next;
        private Node rand;

        public Node(int value) {
            this.value = value;
        }
    }

    public static Node copyListWithRand1(Node head) {
        if (head == null) {
            return null;
        }
        Map<Node, Node> dataMap = new HashMap<>();
        Node cur = head;
        // 将所有节点解耦出来,只保留他的值,不保留引用关系
        while (cur != null){
            dataMap.put(cur,new Node(cur.value));
            cur = cur.next;
        }
        // 再把引用关系给补充上(下一个节点、rand节点)
        cur = head;
        while (cur != null){
            dataMap.get(cur).next = dataMap.get(cur.next);
            dataMap.get(cur).rand = dataMap.get(cur.rand);
            cur = cur.next;
        }
        return dataMap.get(head);
    }


    public static Node copyListWithRand2(Node head) {
        if (head == null){
            return null;
        }
        Node cur = head;
        Node next = null;
        // 复制节点并调整节点next指针
        while (cur != null){
            next = cur.next;
            // 复制一个节点
            cur.next = new Node(cur.value);
            cur.next.next = next;
            cur = next;
        }
        // 调整节点的rand指针
        cur = head;
        Node curCopy = null;
        while (cur != null){
            next = cur.next.next;
            curCopy = cur.next;
            curCopy.rand = cur.rand == null ? null : cur.rand.next;
            cur = next;
        }
        // 分离原始节点和复制节点
        Node res = head.next;
        cur = head;
        while (cur != null){
            next = cur.next.next;
            curCopy= cur.next;
            cur.next = next;
            curCopy.next = next != null ? next.next : null;
            cur = next;
        }
        return res;
    }

    public static void printRandLinkedList(Node head) {
        Node cur = head;
        System.out.print("order: ");
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
        cur = head;
        System.out.print("rand:  ");
        while (cur != null) {
            System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = null;
        Node res1 = null;
        Node res2 = null;
        printRandLinkedList(head);
        res1 = copyListWithRand1(head);
        printRandLinkedList(res1);
        res2 = copyListWithRand2(head);
        printRandLinkedList(res2);
        printRandLinkedList(head);
        System.out.println("=========================");

        head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);

        head.rand = head.next.next.next.next.next; // 1 -> 6
        head.next.rand = head.next.next.next.next.next; // 2 -> 6
        head.next.next.rand = head.next.next.next.next; // 3 -> 5
        head.next.next.next.rand = head.next.next; // 4 -> 3
        head.next.next.next.next.rand = null; // 5 -> null
        head.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4

        printRandLinkedList(head);
        res1 = copyListWithRand1(head);
        printRandLinkedList(res1);
        res2 = copyListWithRand2(head);
        printRandLinkedList(res2);
        printRandLinkedList(head);
        System.out.println("=========================");

    }

}

二:两个单链表相交的节点

2.1:题目描述

【题目】 在本题中,单链表可能有环,也可能无环。给定两个
单链表的头节点 head1和head2,这两个链表可能相交,也可能
不相交。请实现一个函数, 如果两个链表相交,请返回相交的
第一个节点;如果不相交,返回null 即可。

要求:如果链表1
的长度为N,链表2的长度为M,时间复杂度请达到 O(N+M),额外
空间复杂度请达到O(1)。

2.2:题目分析

两个单链表A、B,这里的相交指的是内存地址相同

@1两个单链表都无环            可能相交、可能不相交
@2两个单链表一个有环一个无环   不可能相交
@3两个单链表有环             可能相交可能不相交

所以先判断两个链表有环不

@1情况:
如果两个都无环,如果相交,他们相交后的节点是相同的,所以相交后的个数也是相同的
所以我们可以分为 长链表、短链表
让长链表先走比较长的一部分,待两个链表长度都一样,再逐一比较,直到为空

@2情况:
直接返回null、不可能相交

@3情况:
如果两个有环单链表相交分为两种情况
1)入环节点相同 像数字“6 6”这个样子
2)入环节点不同 像6/这个样子,单斜杠和6共用下面那个圈
如果是1)我们只需要像@1一样,分为长短链表,只不过是到最后一个节点结束,
因为有环,不能到达最后一个节点,只能到入环节点那个位置
如果是2)只需要两个入环节点遍历即可

2.3:Java代码

package xyz.fudongyang.basic.class_03.my;

public class Code_14_FindFirstIntersectNode1 {

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null){
            return null;
        }
        // 得到每个单链表的入环节点,如果没有环,返回null
        Node head1LoopNode = getLoopNode(head1);
        Node head2LoopNode = getLoopNode(head2);
        // 两个无环单链表情况
        if (head1LoopNode == null && head2LoopNode == null){
            return noLoop(head1,head2);
        }
        // 两个有环单链表的情况
        if (head1LoopNode != null && head2LoopNode != null){
            return bothLoop(head1,head1LoopNode,head2,head2LoopNode);
        }
        // 一个有环一个无环单链表不可能相交
        return null;
    }

    public static Node getLoopNode(Node head) {
        // 两个节点构不成环
        if (head == null || head.next == null || head.next.next == null){
            return null;
        }
        Node cur1 = head;
        Node cur2 = head;
        do {
            if (cur2.next == null || cur2.next.next == null){
                return null;
            }
            cur1 = cur1.next;
            cur2 = cur2.next.next;
        }while (cur1 != cur2);
        // 如果存在环,则快指针从头节点开始,慢指针从上次相交位置开始,相遇的一个节点就是入环节点
        cur2 = head;
        while (cur1 != cur2){
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    public static Node noLoop(Node head1, Node head2) {
        // 两个无环的单链表求交点 如果相交,那么相交的点之后两个链表的点都相等
        if (head1 == null || head2 == null){
            return null;
        }
        // 加入两个链表相交,我需要求出长链表,让长链表先走多余的部分,再进行比较
        int n = 0;
        Node cur1 = head1;
        Node cur2 = head2;
        while (cur1 != null){
            n++;
            cur1 = cur1.next;
        }
        while (cur2 != null){
            n--;
            cur2 = cur2.next;
        }
        // 长链表
        cur1 = n > 0 ? head1 : head2;
        // 短链表
        cur2 = cur1 == head1 ? head2 : head1;
        n = Math.abs(n);
        // 长链表先走n步
        while (n>0){
            n--;
            cur1 = cur1.next;
        }
        // 长短链表一起走
        while (cur1 != null){
            if (cur1 == cur2){
                return cur1;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }

    public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        // 两个有环单链表如何判断相交的节点
        // 如果入环的节点相同求相交的节点,求出未入环的相交的节点即可
        if (loop1 == loop2){
            int n = 0;
            Node cur1 = head1;
            Node cur2 = head2;
            while (cur1 != loop1){
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2){
                n--;
                cur2 = cur2.next;
            }
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n>0){
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2){
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        }else {
            // 如果入环节点不相同,求相交的节点,有可能是两个个”6 6“形状 有可能是 ”6/" 这个形状
            // 遍历A链表的入环节点,看看能不能遇到B链表的入环节点
            Node cur1 = loop1.next;
            while (cur1 != loop1){
                if (cur1 == loop2){
                    return loop1;
                }
                cur1 = cur1.next;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

    }

}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-12 16:53:24  更:2021-08-12 16:54:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 4:15:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码