力扣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;
	}
}
相关推荐
wenyq72 小时前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
青 春 记 忆3 小时前
LeetCode 142. 环形链表 II|Python 解法详解
python·leetcode·链表
从琳开始9898 小时前
优选算法——双指针(算法原理+力扣题)
算法·leetcode·职场和发展
从琳开始9899 小时前
优选算法——滑动窗口(概念+解题模板+LeetCode例题讲解)
算法·leetcode·职场和发展
Nil2089 小时前
leetcode 94二叉树的中序遍历
算法·leetcode·职场和发展
圣保罗的大教堂10 小时前
leetcode 1386. 安排电影院座位 中等
leetcode
圣保罗的大教堂11 小时前
leetcode 3069. 将元素分配到两个数组中 I 简单
leetcode
Nil20811 小时前
leetcode 146LRU缓存
算法·leetcode·缓存
rannn_11112 小时前
【力扣hot100】图论专题+模板|DFS、BFS、拓扑排序...
java·算法·leetcode·深度优先·图论
学习星球13 小时前
【LeetCode算法题精讲】二分查找精讲
java·数据结构·算法·leetcode·职场和发展·图搜索