链表经典面试题

news2024/9/23 17:25:22

1 回文链表

1.1 判断方法
  1. 第一种(笔试):
    • 链表从中间分开,把后半部分的节点放到栈中
    • 从链表的头结点开始,依次和弹出的节点比较
  2. 第二种(面试):
    • 反转链表的后半部分,中间节点的下一节点指向空
    • 两个指针分别从链表的头和尾出发比较
    • 直到左边节点到空或两个节点都到空停止
    • 判断结果出来后,要把链表后半部分反转回去。
1.2 代码实现
ublic class IsPalindromList {
    public static class Node {
        public int val;
        public Node next;
        public Node(int data) {
            val = data;
            next = null;
        }
    }

    public static boolean isPalindrom1(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Stack<Node> stack = new Stack<>();
        Node mid = head;
        Node fast = head;
        while (fast.next != null && fast.next.next != null) {
            mid = mid.next;
            fast = fast.next.next;
        }
        while (mid.next != null) {
            mid = mid.next;
            stack.add(mid);
        }
        Node i = head;
        boolean ans = true;
        while (stack.size() != 0) {
            if (i.val != stack.pop().val) {
                ans = false;
                break;
            }
            i = i.next;
        }
        return ans;
    }
    public static boolean isPalindrom2(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        // 找到中间节点
        Node mid = head;
        Node fast = head;
        while (fast.next != null && fast.next.next != null) {
            mid = mid.next;
            fast = fast.next.next;
        }
        Node cur = mid;
        // 后边段链表反转
        Node pre = null;
        Node next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        Node left = head;
        Node right = pre;
        boolean ans = true;
        while (left != null && right != null) {
            if (left.val != right.val) {
                ans = false;
                break;
            }
            left = left.next;
            right = right.next;
        }

        cur = pre;
        pre = null;
        next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return ans;
    }


    public static void main(String[] args) {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(2);
        head.next.next.next.next = new Node(1);
        head.next.next.next.next.next = new Node(0);

        System.out.println(isPalindrom2(head));
        System.out.println(isPalindrom1(head));
    }

}

2 荷兰国旗

链表的荷兰国旗问题,给定一个链表头节点,一个划分值

  • 把链表中小于划分值的放在左边
  • 等于划分值的放在中间
  • 大于划分值的放在右边。
2.1 解决方法

(其实我第一次写的时候第二种方法比第一种方法写的快,因为思路简单,不绕。)

  1. 第一种(笔试):
    • 把链表的所有节点放在一个节点数组中
    • 对这个数组做partition过程
    • 之后把数组每个节点连起来
  2. 第二种(面试):
    • 定义有六个节点,分别代表:大于、等于和小于区域的头和尾
    • 链表开始遍历,比划分值小的连在小于区域下方,同时断开节点和之前链表的关系,指向空
    • 等于和大于同理
    • 遍历完链表之后,开始把小于区域的尾巴和等于区域的头连接,等于区域的尾巴和大于区域的头连接
2.2 代码实现
public class SmallEqualBig {
    public static class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
            next = null;
        }
    }

    public static Node listPartition1(Node head, int pivot) {
        if (head == null || head.next == null) {
            return head;
        }
        int index = 1;
        Node cur = head;
        while (cur.next != null) {
            index++;
            cur = cur.next;
        }
        Node[] arr = new Node[index];
        cur = head;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = cur;
            cur = cur.next;
        }
        partition(arr,pivot);
        for (index = 1; index != arr.length; index++) {
            arr[index - 1].next = arr[index];
        }
        arr[index - 1].next = null;
        return arr[0];
    }

    public static Node listPartition2(Node head, int pivot) {
        if (head == null || head.next == null) {
            return head;
        }
        Node smallHead = null;
        Node smallTail = null;
        Node equalHead = null;
        Node equalTail = null;
        Node bigHead = null;
        Node bigTail = null;
        Node next = null;
        while (head != null) {
            next = head.next;
            head.next = null;
            if (head.val < pivot) {
                if (smallHead == null) {
                    smallHead = head;
                    smallTail = head;
                }else {
                    smallTail.next = head;
                    smallTail = smallTail.next;
                }
            } else if (head.val == pivot) {
                if (equalHead == null) {
                    equalHead = head;
                    equalTail = head;
                }else {
                    equalTail.next = head;
                    equalTail = equalTail.next;
                }
            }else {
                if (bigHead == null) {
                    bigHead = head;
                    bigTail = head;
                }else {
                    bigTail.next = head;
                    bigTail = bigTail.next;
                }
            }
            head = next;
        }
        if (smallTail != null) {
            smallTail.next = equalHead;
            equalTail = equalTail == null ? smallTail : equalTail;
        }
        if (equalTail != null) {
            equalTail.next = bigHead;
        }
        return smallHead == null ? (equalHead == null ? bigHead : equalHead) : smallHead;
    }

    private static void partition(Node[] arr, int pivot) {
        int small = -1;
        int big = arr.length;
        int index = 0;
        while (index < big) {
            if(arr[index].val < pivot) {
                swap(arr, index++, ++small);
            } else if (arr[index].val > pivot) {
                swap(arr, index++, --big);
            }else {
                index++;
            }
        }
    }

    private static void swap(Node[] arr, int i, int j) {
        Node temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        Node head = new Node(9);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(6);
        head.next.next.next.next = new Node(4);
        head.next.next.next.next.next = new Node(5);
        head.next.next.next.next.next.next = new Node(2);

        Node node = listPartition1(head, 2);

        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();

        Node head2 = new Node(9);
        head2.next = new Node(1);
        head2.next.next = new Node(2);
        head2.next.next.next = new Node(6);
        head2.next.next.next.next = new Node(4);
        head2.next.next.next.next.next = new Node(5);
        head2.next.next.next.next.next.next = new Node(2);

        Node node1 = listPartition2(head2, 2);
        while (node1 != null) {
            System.out.print(node1.val + " ");
            node1 = node1.next;
        }

    }
}

3 复制链表

复制特殊链表,单链表中加了个rand指针,可能指向任意一个节点,也可能指向null

给定这个链表的头节点,完成链表的复制

返回复制的新链表的头节点。时间复杂度O(N),额外空间复杂度O(1)

3.1 解决方法
  1. 方法一(笔试):

    • 用HashMap来解决,key是原数组节点,value是复制的数组节点
  2. 方式二(面试):

    • 将复制的新节点插入到原数组的节点中,如1–>2–>3,变成1–>(copy)1–>2–>(copy)2–>3–>(copy)3

    • 复制节点的rand指针指向就是原数组的节点的rand指针的下一节点(下图所示)

    • 把复制数组拿出来的时候注意复制数组连起来还要把原数组连起来

      image-20231109141535414

3.2 代码实现
public class CopyListWithRandom {
    public static class Node {
        public int val;
        public Node next;
        public Node rand;
        public Node(int val) {
            this.val = val;
        }
    }

    // map方法
    public static Node copyListWithRandom(Node head) {
        if (head == null) {
            return null;
        }
        HashMap<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        Node ans = map.get(head);
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).rand = map.get(cur.rand);
            cur = cur.next;
        }
        return ans;
    }

    // 不借助容器
    public static Node copyListWithRandom2(Node head) {
        if (head == null) {
            return null;
        }
        Node cur = head;
        Node next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = new Node(cur.val);
            cur.next.next = next;
            cur = next;
        }
        Node copyNode = null;
        cur = head;
        while (cur != null) {
            copyNode = cur.next;
            copyNode.rand = cur.rand == null ? null : cur.rand.next;
            cur = copyNode.next;
        }
        Node ans = head.next;
        cur = head;
        while (cur != null) {
            next = cur.next.next;
            copyNode = cur.next;
            copyNode.next = next == null ? null : next.next;
            cur.next = next;
            cur = next;
        }
        return ans;
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        head.next = node2;
        node2.next = node3;
        head.rand = node3;
        node2.rand = head;

        Node node = copyListWithRandom(head);
        while (node != null) {
            System.out.println(node.val + " ");
            System.out.println(node == head);
            node = node.next;
            head = head.next;
        }
    }
}

4 链表相交

给定两个可能有环也可能无环的单链表,给定头节点1和头节点2.
请实现一个函数,如果两个链表相交,请返回相交的第一个节点,如果不相交,返回null
时间复杂度O(N),额外空间复杂度O(1)

4.1 解决方法
  1. 先判断两个链表是否有环
  2. 两个都无环
    • 如果两个链表的末尾节点相等,那么就是相交的,用指针先把长的链表走完两个链表的差值后,两个链表开始同时走,直到两个节点相等,就是相交的点
    • 如果末尾节点不相等,那么两个链表不相交
  3. 一个有环一个无环:两个链表不会有相交点
  4. 两个都有环
    • 判断两个链表进环的第一个节点是否是相同的
    • 相同:相交,且相交点就在无环的部分中,参考2
    • 不相同:从一个链表的入环节点开始找,如果找到了第二个链表的入环节点,那么就相交,返回两个入环节点任意一个,如果转一圈到第一个链表的入环节点还没有找到第二个入环节点,此时不相交。
4.2代码实现
public class FindFirstIntersectNode {
    public static class Node {
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }
    }

    public static Node getIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);
        if (loop1 == null && loop2 == null) {
            return noLoop(head1, head2);
        }
        if (loop1 != null && loop2 != null) {
            return bothLoop(head1, loop1, head2, loop2);
        }
        return null;
    }

    private static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
        Node cur1 = head1;
        Node cur2 = head2;
        if (loop1 == loop2) {
            int n = 0;
            while (cur1.next != null) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2.next != null) {
                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 {
            cur1 = loop1.next;
            while (cur1 != loop1) {
                if (cur1 == loop2) {
                    return loop2;
                }
                cur1 = cur1.next;
            }
            return null;
        }
    }

    private static Node noLoop(Node head1, Node head2) {
        Node cur1 = head1;
        Node cur2 = head2;
        int n = 0;
        while (cur1.next != null) {
            n++;
            cur1 = cur2.next;
        }
        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }
        if (cur1 != cur2) {
            return null;
        }
        cur1 = n < 0 ? head2 : head1;
        cur2 = cur1 == head2 ? head1 : head2;
        n = Math.abs(n);
        while (n != 0) {
            n--;
            cur1 = cur1.next;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    private static Node getLoopNode(Node head) {
        if (head.next == null || head.next.next ==null) {
            return null;
        }
        Node slow = head.next;
        Node fast = head.next.next;
        while (fast != slow) {
            if (fast == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }


    public static void main(String[] args) {
        Node head = new Node(0);
        head.next = new Node(1);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(4);
        head.next.next.next.next.next = head.next.next;

        Node head1 = new Node(0);
        head1.next = new Node(1);
        head1.next.next = new Node(2);
        head1.next.next.next = new Node(3);
        head1.next.next.next.next = new Node(4);
        head1.next.next.next.next.next = head.next.next.next;

        Node intersectNode = getIntersectNode(head, head1);
        System.out.println(intersectNode.val);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1248824.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

用户隐私与游戏体验如何平衡?第二周 Web3 开发者集结精华回顾

由 TinTinLand 联合 Dataverse 、Web3Go 、Subquery 、Cregis 、Litentry、Aspecta、SpaceID、ANOME、VARA&Gear、Moonbeam、Mantle、Obelisk 等 10 余家 Web3 项目共同举办的 Web3 开发者赢积分活动已举办至第三周。精彩线上主题活动分享、近距离交流体验互动&#xff0c;…

PostgreSQL Patroni 3.0 新功能规划 2023年 纽约PG 大会 (音译)

开头还是介绍一下群&#xff0c;如果感兴趣PolarDB ,MongoDB ,MySQL ,PostgreSQL ,Redis, Oceanbase, Sql Server等有问题&#xff0c;有需求都可以加群群内有各大数据库行业大咖&#xff0c;CTO&#xff0c;可以解决你的问题。加群请联系 liuaustin3 &#xff0c;&#xff08;…

AlDente Pro v1.22.2(mac电池最大充电限制工具)

AlDente Pro是一款适用于Mac操作系统的小工具&#xff0c;可以帮助您限制电池充电量以延长电池寿命。通常情况下&#xff0c;电池在充满的状态下会继续接受电源充电&#xff0c;这可能会导致电池寿命缩短。使用AlDente Pro&#xff0c;您可以设置电池只充到特定的充电水平&…

TYPE-C、PD原理

一、Type-C简介以及历史 自1998年以来&#xff0c;USB发布至今&#xff0c;USB已经走过20个年头有余了。在这20年间&#xff0c;USB-IF组织发布N种接口状态&#xff0c;包括A口、B口、MINI-A、MINI-B、Micro-A、Micro-B等等接口形态&#xff0c;由于各家产品的喜好不同&#x…

Azkaban极简使用文档

登录 地址: http://服务器ip:8081/, 用户名密码默认都是azkaban 构建项目流程 添加Project 编写工作流文件 在本机新建文件夹如test, 创建一个flow20.project 文件, 内容 azkaban-flow-version: 2.0(固定步骤)编写flow文件, 例如一个最基础的实例 test1.flow nodes:- name…

C语言剔除相关数(ZZULIOJ1204:剔除相关数)

题目描述 一个数与另一个数如果含有相同数字和个数的字符&#xff0c;则称两数相关。现有一堆乱七八糟的整数&#xff0c;里面可能充满了彼此相关的数&#xff0c;请你用一下手段&#xff0c;自动地将其剔除。 输入&#xff1a;多实例测试。每组数据包含一个n(n<1000)&#…

Pbootcms商城插件,支持购物车、订单、支付管理等

pbootcms商城插件上线&#xff0c;可以实现简单的商品管理、商品购买、加入购物车、购物车批量购买、以及后台的订单管理&#xff0c;发货管理、改价功能&#xff0c;支付信息管理等功能&#xff0c;满足商城基本功能后台效果截图&#xff1a; 前端效果截图&#xff1a; 演示网…

【Golang】解决使用interface{}解析json数字会变成科学计数法的问题

在使用解析json结构体的时候&#xff0c;使用interface{}接数字会发现变成了科学计数法格式的数字&#xff0c;不符合实际场景的使用要求。 举例代码如下&#xff1a; type JsonUnmStruct struct {Id interface{} json:"id"Name string json:"name"…

基于Vue+SpringBoot的天沐瑜伽馆管理系统

项目编号&#xff1a; S 039 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S039&#xff0c;文末获取源码。} 项目编号&#xff1a;S039&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 瑜伽课程模块2.3 课…

C语言—指针和数组

写在前 一个指针变量指向某个普通变量&#xff0c;则指针变量就等于普通变量。 指针变量存放的是地址&#xff0c;普通变量存放的是数据。 int * p; int i5,j; p &i;此程序&#xff0c;*pi5&#xff0c;在所有出现 *p 或 i 的位置&#xff0c;两者都可以互相替换。 通过…

无人机巡检如何做到实时识别,从数据到模型全流程解读

在数字化和自动化飞速发展的今天&#xff0c;AI识别算法正在加速进入行业生产系统。 基于巡检数据的智能开发&#xff0c;识别算法突破性进展的核心驱动力在于需求——从全天候巡视的平安城市&#xff0c;到潮汐变化的交通网络&#xff0c;从广阔的水域&#xff0c;到繁忙的街道…

【2023.11.23】JDBC基本连接语法学习➹

1.导入jar包依赖&#xff1a;mysql-connector-java-8.0.27.jar 2.连接数据库&#xff01; 3.无法解析类->导入java.sql.*&#xff0c;&#xff08;将项目方言改为Mysql&#xff09; JDBC&#xff0c;启动&#xff01;&#xff01; public class Main {public static voi…

c语言-浅谈指针(4)

文章目录 1.回调函数概念举例 2.qsort函数qsort的使用 3.通过冒泡排序来模拟qsort函数排序int类型排序结构体类型 这是指针最后一篇了喔&#xff0c;完结撒花 ! 前三篇&#xff1a; 浅谈指针&#xff08;1&#xff09;http://t.csdnimg.cn/JTRjW 浅谈指针&#xff08;2&#xf…

React项目中发生空白但不报错的原因分析和解决?

文章目录 前言组件渲染问题状态管理问题异步操作问题代码错误但未抛出异常如果我们使用的是chorme浏览器的话&#xff0c;可以下载一个开发者工具&#xff0c;例如下图&#xff1a;代码审查使用调试工具日志和输出检查外部依赖异步操作终极大法&#xff0c;不到万不得已不可以使…

问答知识库快速构建技术解析及行业实践

对话式 AI 类产品&#xff0c;已经在各行各业中实现规模化的应用。随着科技创新支撑下的高质量行业发展&#xff0c;人工智能已成为数字经济时代的核心生产力。其中对话式 AI&#xff0c;作为人工智能技术的一个分支&#xff0c;随着深度学习、预训练模型等技术的突破&#xff…

【数据结构/C++】线性表_单链表的基本操作

#include <iostream> using namespace std; // 2. 单链表 // ElemType 的定义 typedef int ElemType; typedef struct LNode {ElemType data;struct LNode *next; } LNode, *LinkList; // 初始化单链表 bool InitList(LinkList &L) {L (LNode *)malloc(sizeof(LNode…

Java自动装箱(autoboxing)和自动拆箱(autounboxing)介绍

Java自动装箱(autoboxing)和自动拆箱(autounboxing)介绍 先回顾一下 Java 中的基本数据类型和包装类。 基本数据类型&#xff08;Primitive Data Types&#xff09;&#xff1a; Java 提供了一组基本数据类型&#xff0c;有8种基本数据类型&#xff1a;byte、short、int、long…

【Spring】MyBatis的操作数据库

目录 一&#xff0c;准备工作 1.1 创建工程 1.2 准备数据 1.3 数据库连接字符串 1.4 创建持久层接口UserInfoMapper 1.5 单元测试 二&#xff0c;注解的基础操作 2.1 打印日志 2.2 参数传递 2.3 增&#xff08;Insert&#xff09; 2.4 删&#xff08;Delete&#x…

华为云之在Linux系统下安装可视化界面

华为云之在Linux系统下安装可视化界面 一、华为云弹性云服务器ECS介绍二、Linux图形化界面介绍三、本次实践介绍3.1 本次实践简介3.2 本次实践环境介绍 四、环境准备工作4.1 预置环境4.2 查看预置环境资源信息 五、连接弹性云服务器ECS5.1 登录华为云5.2 复制ECS弹性公网IP地址…

1|1111

1、指定在每天凌晨4&#xff1a;00将该时间点之前的系统日志信息&#xff08;/var/log/messages &#xff09;备份到目录下/backup&#xff0c;备份后日志文件名显示格式logfileYY-MM-DD-HH-MM 2、配置ssh免密登陆&#xff1a;客户端主机通过redhat用户基于秘钥验证方式进行远…