力扣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;
	}
}
相关推荐
java_nnnn5 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
Navigator_Z6 小时前
LeetCode //C - 1301. Number of Paths with Max Score
c语言·算法·leetcode
Nuanyt8 小时前
LeetCode Hot100 刷题笔记 · 普通数组专题
java·笔记·算法·leetcode
xxxiugou1239 小时前
链表算法题精讲:从两数相加到K组翻转
数据结构·c++·算法·链表
春弦_10 小时前
LeetCode Hot 100 刷题笔记
笔记·leetcode·哈希算法
凯瑟琳.奥古斯特13 小时前
力扣1015C++解法技巧
c++·算法·leetcode·链表·职场和发展
文丑颜不良啊13 小时前
LeetCode626~650题目解析
java·leetcode
文丑颜不良啊14 小时前
LeetCode601~625题目解析
java·leetcode
文丑颜不良啊14 小时前
LeetCode651~675题目解析
java·leetcode
Xin77015 小时前
LeetCode 78. 子集(回溯法)
leetcode