力扣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;
	}
}
相关推荐
圣保罗的大教堂3 小时前
leetcode 2540. 最小公共值 简单
leetcode
洛水水7 小时前
【力扣100题】53.最长回文子串
算法·leetcode·职场和发展
过期动态8 小时前
【LeetCode 热题 100】盛最多水的容器
java·数据结构·spring boot·算法·leetcode·spring cloud·职场和发展
凌波粒9 小时前
LeetCode--700.二叉搜索树中的搜索(二叉树)
算法·leetcode·职场和发展
洛水水9 小时前
【力扣100题】58.轮转数组
算法·leetcode
风筝在晴天搁浅9 小时前
阿里 LeetCode 876.链表的中间节点
算法·leetcode·链表
玖釉-9 小时前
二叉树展开为链表:从先序遍历到原地指针重排
c++·windows·算法·leetcode·链表
洛水水10 小时前
【力扣100题】52.最小路径和
算法·leetcode
圣保罗的大教堂10 小时前
leetcode 3043. 最长公共前缀的长度 中等
leetcode
菜菜的顾清寒13 小时前
力扣HOT100(34)图论-岛屿数量
算法·leetcode·图论