【Java-数据结构】Java 链表面试题下 “最后一公里”:解决复杂链表问题的致胜法宝

news/2025/2/5 16:28:09/

在这里插入图片描述

我的个人主页
我的专栏Java-数据结构,希望能帮助到大家!!!点赞❤ 收藏❤
在这里插入图片描述
在这里插入图片描述

引言:
Java链表,看似简单的链式结构,却蕴含着诸多有趣的特性与奥秘,等待我们去挖掘。它就像一个神秘的宝藏迷宫,每一个特性都是隐藏在迷宫深处的珍贵宝藏。链表的环,如同迷宫中的循环通道,一旦进入,便可能陷入无尽的循环;链表节点的唯一性与重复性,仿佛迷宫中的岔路,有的道路独一无二,有的却似曾相识;而链表的长度变化,又如同迷宫的动态扩展与收缩。在接下来的题目中,你将化身为勇敢的探险家,深入链表特性的迷宫,运用你的编程智慧,解开一个个谜题。通过检测链表的环、分析节点的重复性以及精准计算链表长度,你将逐渐揭开链表神秘的面纱,领略数据结构背后的奇妙逻辑。

6.编写代码,以给定值x为基准将链表分割成两部分,所有⼩于x的结点排在⼤于或等于x的结点之前—链表分割

题目视图:
在这里插入图片描述

题目详解代码:

java">// 定义链表节点类
class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}
}public class PartitionList {public ListNode partition(ListNode pHead, int x) {// 创建两个虚拟头节点,分别用于存储小于 x 和大于等于 x 的节点ListNode smallDummy = new ListNode(0);ListNode largeDummy = new ListNode(0);// 分别指向两个新链表的当前节点ListNode smallTail = smallDummy;ListNode largeTail = largeDummy;// 遍历原链表ListNode current = pHead;while (current != null) {if (current.val < x) {// 如果当前节点的值小于 x,将其连接到 small 链表的尾部smallTail.next = current;smallTail = smallTail.next;} else {// 如果当前节点的值大于等于 x,将其连接到 large 链表的尾部largeTail.next = current;largeTail = largeTail.next;}// 移动到下一个节点current = current.next;}// 断开 large 链表的尾部,防止出现循环largeTail.next = null;// 将 small 链表和 large 链表连接起来smallTail.next = largeDummy.next;// 返回重新排列后的链表的头节点return smallDummy.next;}public static void main(String[] args) {// 创建测试链表 1 -> 4 -> 3 -> 2 -> 5 -> 2ListNode head = new ListNode(1);head.next = new ListNode(4);head.next.next = new ListNode(3);head.next.next.next = new ListNode(2);head.next.next.next.next = new ListNode(5);head.next.next.next.next.next = new ListNode(2);PartitionList solution = new PartitionList();int x = 3;// 调用 partition 方法进行重新排列ListNode newHead = solution.partition(head, x);// 打印重新排列后的链表ListNode current = newHead;while (current != null) {System.out.print(current.val + " ");current = current.next;}}
}

在这里插入图片描述

7.链表的回⽂结构。题目链接

题目视图:在这里插入图片描述

题目详解代码:

java">package Demo1_28;/*** Created with IntelliJ IDEA.* Description:* User:Lenovo* Date:2025-01-28* Time:20:04*/
// 定义链表节点类
class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}
}public class PalindromeLinkedList {public boolean isPalindrome(ListNode A) {if (A == null || A.next == null) {return true;}// 步骤 1:找到链表的中间节点ListNode slow = A;ListNode fast = A;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast = fast.next.next;}// 步骤 2:反转链表的后半部分ListNode secondHalf = reverseList(slow.next);// 步骤 3:比较链表的前半部分和反转后的后半部分ListNode p1 = A;ListNode p2 = secondHalf;boolean result = true;while (result && p2 != null) {if (p1.val != p2.val) {result = false;}p1 = p1.next;p2 = p2.next;}// 步骤 4:恢复链表的原始结构slow.next = reverseList(secondHalf);return result;}// 反转链表的方法private ListNode reverseList(ListNode head) {ListNode prev = null;ListNode curr = head;while (curr != null) {ListNode nextTemp = curr.next;curr.next = prev;prev = curr;curr = nextTemp;}return prev;}public static void main(String[] args) {// 创建测试链表 1 -> 2 -> 2 -> 1ListNode head = new ListNode(1);head.next = new ListNode(2);head.next.next = new ListNode(2);head.next.next.next = new ListNode(1);PalindromeLinkedList solution = new PalindromeLinkedList();// 调用 isPalindrome 方法判断链表是否为回文结构boolean isPalindrome = solution.isPalindrome(head);System.out.println(isPalindrome);}
}

在这里插入图片描述

8.输⼊两个链表,找出它们的第⼀个公共结点。—题目链接

题目视图:
在这里插入图片描述

题目详解代码:

java">package Demo1_28;/*** Created with IntelliJ IDEA.* Description:* User:Lenovo* Date:2025-01-28* Time:20:08*/
// 定义链表节点类
class ListNode {int val;ListNode next;// 构造函数,用于初始化节点的值ListNode(int x) {val = x;next = null;}
}public class IntersectionOfTwoLinkedLists {// 查找两个链表相交的起始节点的方法public ListNode getIntersectionNode(ListNode headA, ListNode headB) {// 如果其中一个链表为空,直接返回 nullif (headA == null || headB == null) {return null;}// 初始化两个指针分别指向两个链表的头节点ListNode pA = headA;ListNode pB = headB;// 当两个指针不相等时,继续循环while (pA != pB) {// 如果 pA 到达链表 A 的末尾,将其指向链表 B 的头节点pA = pA == null ? headB : pA.next;// 如果 pB 到达链表 B 的末尾,将其指向链表 A 的头节点pB = pB == null ? headA : pB.next;}// 返回相交节点,如果不相交则返回 nullreturn pA;}public static void main(String[] args) {// 创建示例链表// 链表 A: 1 -> 2 -> 3 -> 6 -> 7ListNode headA = new ListNode(1);headA.next = new ListNode(2);headA.next.next = new ListNode(3);// 链表 B: 4 -> 5 -> 6 -> 7ListNode headB = new ListNode(4);headB.next = new ListNode(5);// 创建相交部分ListNode intersection = new ListNode(6);intersection.next = new ListNode(7);// 连接链表 A 和相交部分headA.next.next.next = intersection;// 连接链表 B 和相交部分headB.next.next = intersection;// 创建 IntersectionOfTwoLinkedLists 类的实例IntersectionOfTwoLinkedLists solution = new IntersectionOfTwoLinkedLists();// 调用 getIntersectionNode 方法查找相交节点ListNode result = solution.getIntersectionNode(headA, headB);if (result != null) {System.out.println("相交节点的值为: " + result.val);} else {System.out.println("两个链表不相交");}}
}

在这里插入图片描述

9.给你一个链表的头节点 head ,判断链表中是否有环。—题目链接

题目视图:在这里插入图片描述

题目详解代码:

java">// 定义链表节点类
class ListNode {int val;ListNode next;// 构造函数,用于初始化节点的值ListNode(int x) {val = x;next = null;}
}public class LinkedListCycle {// 判断链表是否有环的方法public boolean hasCycle(ListNode head) {// 如果链表为空或者只有一个节点,肯定没有环if (head == null || head.next == null) {return false;}// 慢指针,初始指向头节点,每次移动一步ListNode slow = head;// 快指针,初始指向头节点的下一个节点,每次移动两步ListNode fast = head.next;// 当慢指针和快指针不相等时,继续循环while (slow != fast) {// 如果快指针到达链表末尾或者快指针的下一个节点是末尾,说明没有环if (fast == null || fast.next == null) {return false;}// 慢指针移动一步slow = slow.next;// 快指针移动两步fast = fast.next.next;}// 如果跳出循环,说明慢指针和快指针相遇了,链表有环return true;}public static void main(String[] args) {// 创建有环链表 1 -> 2 -> 3 -> 4 -> 2(环从节点 2 开始)ListNode node1 = new ListNode(1);ListNode node2 = new ListNode(2);ListNode node3 = new ListNode(3);ListNode node4 = new ListNode(4);// 构建链表连接关系node1.next = node2;node2.next = node3;node3.next = node4;// 形成环node4.next = node2;// 创建 LinkedListCycle 类的实例LinkedListCycle solution = new LinkedListCycle();// 调用 hasCycle 方法判断链表是否有环boolean result = solution.hasCycle(node1);System.out.println("链表是否有环: " + result);// 创建无环链表 1 -> 2 -> 3 -> 4ListNode nodeA = new ListNode(1);ListNode nodeB = new ListNode(2);ListNode nodeC = new ListNode(3);ListNode nodeD = new ListNode(4);// 构建无环链表连接关系nodeA.next = nodeB;nodeB.next = nodeC;nodeC.next = nodeD;// 再次调用 hasCycle 方法判断无环链表是否有环result = solution.hasCycle(nodeA);System.out.println("链表是否有环: " + result);}
}

在这里插入图片描述

10.给定⼀个链表,返回链表开始⼊环的第⼀个节点。 如果链表⽆环,则返回 NULL

—题目链接

题目视图:在这里插入图片描述

题目详解代码:

java">package Demo1_28;/*** Created with IntelliJ IDEA.* Description:* User:Lenovo* Date:2025-01-28* Time:20:15*/
// 定义链表节点类
class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}
}public class LinkedListCycleII {public ListNode detectCycle(ListNode head) {// 如果链表为空或只有一个节点,肯定没有环if (head == null || head.next == null) {return null;}// 慢指针,初始指向头节点,每次移动一步ListNode slow = head;// 快指针,初始指向头节点,每次移动两步ListNode fast = head;boolean hasCycle = false;// 寻找是否有环while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;// 快慢指针相遇,说明有环if (slow == fast) {hasCycle = true;break;}}// 如果没有环,返回 nullif (!hasCycle) {return null;}// 慢指针重新指向头节点slow = head;// 快慢指针都以每次一步的速度移动,再次相遇的节点就是环的入口节点while (slow != fast) {slow = slow.next;fast = fast.next;}return slow;}public static void main(String[] args) {// 创建有环链表 1 -> 2 -> 3 -> 4 -> 2(环从节点 2 开始)ListNode node1 = new ListNode(1);ListNode node2 = new ListNode(2);ListNode node3 = new ListNode(3);ListNode node4 = new ListNode(4);// 构建链表连接关系node1.next = node2;node2.next = node3;node3.next = node4;// 形成环node4.next = node2;// 创建 LinkedListCycleII 类的实例LinkedListCycleII solution = new LinkedListCycleII();// 调用 detectCycle 方法找到环的入口节点ListNode result = solution.detectCycle(node1);if (result != null) {System.out.println("环的入口节点的值为: " + result.val);} else {System.out.println("链表中没有环");}// 创建无环链表 1 -> 2 -> 3 -> 4ListNode nodeA = new ListNode(1);ListNode nodeB = new ListNode(2);ListNode nodeC = new ListNode(3);ListNode nodeD = new ListNode(4);// 构建无环链表连接关系nodeA.next = nodeB;nodeB.next = nodeC;nodeC.next = nodeD;// 再次调用 detectCycle 方法判断无环链表是否有环result = solution.detectCycle(nodeA);if (result != null) {System.out.println("环的入口节点的值为: " + result.val);} else {System.out.println("链表中没有环");}}
}

在这里插入图片描述
所有的链表题目就分享到着了继续加油❤👍!!!


http://www.ppmy.cn/news/1569549.html

相关文章

RabbitMQ快速上手及入门

概念 概念&#xff1a; publisher&#xff1a;生产者&#xff0c;也就是发送消息的一方 consumer&#xff1a;消费者&#xff0c;也就是消费消息的一方 queue&#xff1a;队列&#xff0c;存储消息。生产者投递的消息会暂存在消息队列中&#xff0c;等待消费者处理 exchang…

一篇关于高等数理统计结合机器学习论文的撰写(如何撰写?)

前言 在大学或者研究生阶段&#xff0c;大家可能都会遇到一个问题就是&#xff0c;在上高等数理统计课程时&#xff0c;老师总会让同学们写一些大作业&#xff0c;比如论文什么的&#xff0c;接下来我会从计算机领域的角度&#xff0c;带领大家开启一篇从0到1的高等数理统计文…

DeSpread:Berachain 生态特点探讨与核心项目一览

撰文&#xff1a;DeSpread 引 言 Berachain 是一个 Layer 1 网路&#xff0c;以 PoL&#xff08;Proof of Liquidity&#xff09;共识机制为特色&#xff0c;使验证者、流动性提供者和协议的利益一致。目前&#xff0c;Berachain 正在进行第二个测试网路「bArtio Testnet」&a…

HTML5 技术深度解读:本地存储与地理定位的最佳实践

系列文章目录 01-从零开始学 HTML&#xff1a;构建网页的基本框架与技巧 02-HTML常见文本标签解析&#xff1a;从基础到进阶的全面指南 03-HTML从入门到精通&#xff1a;链接与图像标签全解析 04-HTML 列表标签全解析&#xff1a;无序与有序列表的深度应用 05-HTML表格标签全面…

使用LLaMA-Factory对AI进行认知的微调

使用LLaMA-Factory对AI进行认知的微调 引言1. 安装LLaMA-Factory1.1. 克隆仓库1.2. 创建虚拟环境1.3. 安装LLaMA-Factory1.4. 验证 2. 准备数据2.1. 创建数据集2.2. 更新数据集信息 3. 启动LLaMA-Factory4. 进行微调4.1. 设置模型4.2. 预览数据集4.3. 设置学习率等参数4.4. 预览…

关于19C的审计日志

最近在用19C做测试时&#xff0c;发现了个问题&#xff0c;即在audit_trail 为DB的情况下&#xff0c;模拟用户登录登出过程&#xff0c;但是 aud$ 表里没记录。 查阅资料得知: 12.1.0.1 开始情况使用的 统一审计&#xff08;Unified Auditing&#xff09;&#xff0c;默认开…

数据库索引:秋招面试中的经典高频题目 [特殊字符](索引原理/操作/优缺点/B+树)

在数据库的秋招面试中&#xff0c;索引&#xff08;Index&#xff09;是一个经典且高频的题目。索引的作用类似于书中的目录&#x1f4d6;&#xff0c;它能够显著加快数据库查询的速度。本文将深入探讨索引的概念、作用、优缺点以及背后的数据结构&#xff0c;帮助你从原理到应…

请解释 Java 中的 IO 和 NIO 的区别,以及 NIO 如何实现多路复用?

Java中的IO和NIO是两种不同的输入输出处理方式&#xff0c;它们在设计理念、实现方式、性能特点和应用场景上有着显著的差异。 下面我将详细解释Java中的IO和NIO的区别&#xff0c;以及NIO如何实现多路复用&#xff0c;并提供一些日常开发中的使用建议和注意事项。 Java中的I…