力扣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;
	}
}
相关推荐
alphaTao17 小时前
LeetCode 每日一题 2025/12/22-2025/12/28
算法·leetcode
小白菜又菜18 小时前
Leetcode 1523. Count Odd Numbers in an Interval Range
算法·leetcode
小白菜又菜19 小时前
Leetcode 944. Delete Columns to Make Sorted
算法·leetcode
橘颂TA20 小时前
【剑斩OFFER】算法的暴力美学——链表相加(二)
数据结构·链表·牛客·结构与算法
报错小能手20 小时前
数据结构 可扩展哈希
数据结构·哈希算法·散列表
Swift社区20 小时前
LeetCode 458 - 可怜的小猪
算法·leetcode·职场和发展
wbs_scy21 小时前
C++:unordered_map/unordered_set 使用指南(差异、性能与场景选择)
开发语言·c++·哈希算法
Dream it possible!21 小时前
LeetCode 面试经典 150_分治_将有序数组转换为二叉搜索树(105_108_C++_简单)(递归)
c++·leetcode·面试
Q741_14721 小时前
C++ 栈 模拟 力扣 227. 基本计算器 II 题解 每日一题
c++·算法·leetcode·模拟
im_AMBER21 小时前
Leetcode 88 K 和数对的最大数目
数据结构·c++·笔记·学习·算法·leetcode