力扣hot100 环形链表 快慢指针 哈希 数学公式

Problem: 142. 环形链表 II

文章目录

思路

👨‍🏫 参考题解

Code

⏰ 时间复杂度: O ( n ) O(n) O(n)

🌎 空间复杂度: O ( 1 ) O(1) O(1)

Java 复制代码
/**
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
	public ListNode detectCycle(ListNode head)
	{

		ListNode f = head;
		ListNode s = head;
		while (f != null && f.next != null)
		{
			f = f.next.next;
			s = s.next;
			if (f == s)// 到相遇点了
			{
				while (s != head)//两者相等即走到了入环点
				{
					s = s.next;// s 走 相遇点到入环点 的路
					head = head.next;// head 走 起点到入环点的路
				}
				return s;
			}
		}
		return null;
	}
}
相关推荐
Hi李耶10 小时前
【LeetCode】9-回文数
算法·leetcode·职场和发展
海绵天哥14 小时前
LeetCode Hot 100 | 链表(下)· 分组翻转与设计(C++ 题解)
c++·leetcode·链表
神明不懂浪漫1 天前
【第三章】链表
开发语言·数据结构·经验分享·笔记·链表
tkevinjd1 天前
力扣131-分割回文串
算法·leetcode·深度优先
zander2581 天前
LeetCode 78. 子集
算法·leetcode·深度优先
Tisfy1 天前
LeetCode 3014.输入单词需要的最少按键次数 I:遍历 / if-else计算(比纯数学公式写起来麻烦但好想)
数学·算法·leetcode·字符串·题解·贪心
tkevinjd1 天前
力扣239-滑动窗口最大值
算法·leetcode·职场和发展
圣保罗的大教堂1 天前
leetcode 628. 三个数的最大乘积 简单
leetcode
良木林1 天前
矩阵 - LeetCode hot 100
线性代数·算法·leetcode·矩阵
兰令水2 天前
hot100【查缺补漏+打印线程abc】【2026.7.30打卡-java版本】
java·算法·leetcode