力扣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;
	}
}
相关推荐
小武~7 小时前
Leetcode 每日一题C 语言版 -- 45 jump game ii
c语言·算法·leetcode
程序员-周李斌11 小时前
ConcurrentHashMap 源码分析
java·开发语言·哈希算法·散列表·开源软件
天赐学c语言11 小时前
12.6 - K个一组翻转链表 && C 编译到执行的4个阶段
数据结构·c++·链表·c编译
leoufung11 小时前
用 DFS 拓扑排序吃透 LeetCode 210:Course Schedule II
算法·leetcode·深度优先
麒qiqi12 小时前
【数据结构核心篇】树与哈希(Hash)的原理、特性及实战应用
数据结构·算法·哈希算法
Swift社区12 小时前
LeetCode 443. 压缩字符串
leetcode·职场和发展·蓝桥杯
ada7_12 小时前
LeetCode(python)——543.二叉树的直径
数据结构·python·算法·leetcode·职场和发展
sprintzer13 小时前
11.26-12.05力扣栈刷题
算法·leetcode·职场和发展
sin_hielo13 小时前
leetcode 3578
数据结构·算法·leetcode
前端小白在前进13 小时前
力扣刷题:无重复字符的最长子串
算法·leetcode·职场和发展