力扣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;
	}
}
相关推荐
土司大王16 小时前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
zander25818 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
欧叶冲冲冲18 小时前
Python常见数据结构的CRUD(LeetCode高频版速查)
数据结构·python·leetcode
圣保罗的大教堂18 小时前
leetcode 3622. 判断整除性 简单
leetcode
_Narcissus_19 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
Lyyaoo.19 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
圣保罗的大教堂20 小时前
leetcode 2996. 大于等于顺序前缀和的最小缺失整数 简单
leetcode
圣保罗的大教堂21 小时前
leetcode 3702. 按位异或非零的最长子序列 中等
leetcode
青 春 记 忆1 天前
LeetCode 206. 反转链表|Python 解法详解
python·leetcode·链表