当前位置:网站首页>leetcode/链表中环的入口节点
leetcode/链表中环的入口节点
2022-08-09 16:24:00 【xcrj】
代码
package com.xcrj;
import java.util.HashSet;
import java.util.Set;
/** * 剑指 Offer II 022. 链表中环的入口节点 * 给定一个链表,返回链表开始入环的第一个节点。 从链表的头节点开始沿着 next 指针进入环的第一个节点为环的入口节点。如果链表无环,则返回 null。 * 链表有没有环,环的入口结点是,环是从尾部结点next指向前面的某个结点(环入口结点) */
public class Solution22 {
/** * 有环证明以前访问过 * 使用hash表记录之前访问过的结果 */
public ListNode detectCycle1(ListNode head) {
if (head == null) {
return null;
}
Set<ListNode> visited = new HashSet<>(3);
ListNode p = head;
while (p.next != null) {
if (visited.contains(p)) {
return p;
} else {
visited.add(p);
}
p = p.next;
}
return null;
}
/** * 快慢指针,两次相遇,第一次slow和fast相遇,第二次slow和p相遇得到环入口结点 * <p> * 式子1: * slow指针走一步 fast指针走两步 因此fast是slow的两倍速 * 所以,2*slow=fast * <p> * 式子2: * 设环外距离为a,slow指针环内移动距离为b,环距离总长为b+c, * slow指针移动距离为 a+b,slow指针在环内走了b步 * fast指针移动距离为 a+n(b+c)+b,fast指针绕环n圈并走了b步之后与slow相遇 * <p> * 式子1、式子2: * 所以,2*(a+b)=a+n(b+c)+b * !!!知道,当slow指针和fast指针相遇之后,求环入口结点,即求a的长度 * 根据上式推出 a=c+(n-1)(b+c)即 当slow和fast相遇之后,指针再走c加上n-1圈就可以得到环入口结点,slow指针再走c加上n-1圈就可以得到环入口结点 */
public ListNode detectCycle2(ListNode head) {
// 特殊情况处理
if (head == null) {
return null;
}
// slow fast指针移动
ListNode slow = head;
ListNode fast = head;
// 因为fast走的块
while (fast != null) {
slow = slow.next;
// !!next不为nullfast才有next.next
if (fast.next != null) {
fast = fast.next.next;
} else {
return null;
}
// slow和fast相遇
if (slow == fast) {
// !!!相遇之后slow走c+(n-1)圈=a,即让slow从相遇点开始走c+(n-1)(b+c),让p从head开始走a,slow和p相遇
ListNode p = head;
// slow和p相遇
while (slow != p) {
slow = slow.next;
p = p.next;
}
return p;
}
}
return null;
}
public static void main(String[] args) {
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/c32eOV/solution/lian-biao-zhong-huan-de-ru-kou-jie-dian-vvofe/
来源:力扣(LeetCode)
边栏推荐
猜你喜欢

B50 - 基于51单片机的儿童成长管理系统

央企施工企业数字化转型的灵魂是什么

Lagrange插值公式matlab实现

快捷键修改typora字体----自制脚本

云服务的分类和应用

uniapp电影购票选座系统源码

在 C# 中如何检查参数是否为 null

WinForm(三)揭开可视化控件的面纱

SQL trill interview: send you a universal template, to?(key, each user to log on to the maximum number of consecutive monthly)

【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例...
随机推荐
贫血模型与充血模型
110+ public professional datasets summarized
uniapp电影购票选座系统源码
After the WeChat developer tool program is developed, no error is reported, but the black screen "recommended collection"
.NET 6 study notes (4) - Solve the Nullable warning in VS2022
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
Account opening requirements and exemptions for special futures such as crude oil
微服务:事务管理
PADS生成位号图
What you should know about futures account opening
CocosCreator接入微信小游戏
安装MySQL的最后一步第四个红叉怎么解决
ABP详细教程——模块类
.NET 6学习笔记(4)——解决VS2022中Nullable警告
电子产品硬件开发中存在的问题
【教程3】疯壳·ARM功能手机-整板资源介绍
现在,怎么挑选舞台租赁LED显示屏?
[ Kitex 源码解读 ] 请求重试
dichotomy
.NET MAUI 跨平台应用开发 I|.NET MAUI 跨平台基础