力扣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;
	}
}
相关推荐
Lris-KK3 小时前
力扣Hot100--94.二叉树的中序遍历、144.二叉树的前序遍历、145.二叉树的后序遍历
python·算法·leetcode
坚持编程的菜鸟4 小时前
LeetCode每日一题——螺旋矩阵
c语言·算法·leetcode·矩阵
(●—●)橘子……4 小时前
记力扣2009:使数组连续的最少操作数 练习理解
数据结构·python·算法·leetcode
GalaxyPokemon4 小时前
LeetCode - 1171.
算法·leetcode·链表
iナナ5 小时前
Java优选算法——位运算
java·数据结构·算法·leetcode
Han.miracle6 小时前
数据结构二叉树——层序遍历&& 扩展二叉树的左视图
java·数据结构·算法·leetcode
L_09079 小时前
【Algorithm】Day-4
c++·算法·leetcode
代码充电宝9 小时前
LeetCode 算法题【简单】20. 有效的括号
java·算法·leetcode·面试·职场和发展
海琴烟Sunshine9 小时前
leetcode 119. 杨辉三角 II python
算法·leetcode·职场和发展
玉夏10 小时前
【每日算法C#】爬楼梯问题 LeetCode
算法·leetcode·c#