LeetCode 题解工作台
环形链表 II
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null 。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置( 索引从 0 开始 )。如果 …
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 链表指针操作
答案摘要
我们先利用快慢指针判断链表是否有环,如果有环的话,快慢指针一定会相遇,且相遇的节点一定在环中。 如果没有环,快指针会先到达链表尾部,直接返回 `null` 即可。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 链表指针操作 题型思路
题目描述
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
示例 1:

输入:head = [3,2,0,-4], pos = 1 输出:返回索引为 1 的链表节点 解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:

输入:head = [1,2], pos = 0 输出:返回索引为 0 的链表节点 解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:

输入:head = [1], pos = -1 输出:返回 null 解释:链表中没有环。
提示:
- 链表中节点的数目范围在范围
[0, 104]内 -105 <= Node.val <= 105pos的值为-1或者链表中的一个有效索引
进阶:你是否可以使用 O(1) 空间解决此题?
解题思路
方法一:快慢指针
我们先利用快慢指针判断链表是否有环,如果有环的话,快慢指针一定会相遇,且相遇的节点一定在环中。
如果没有环,快指针会先到达链表尾部,直接返回 null 即可。
如果有环,我们再定义一个答案指针 指向链表头部,然后让 和慢指针一起向前走,每次走一步,直到 和慢指针相遇,相遇的节点即为环的入口节点。
为什么这样能找到环的入口节点呢?
我们不妨假设链表头节点到环入口的距离为 ,环入口到相遇节点的距离为 ,相遇节点到环入口的距离为 ,那么慢指针走过的距离为 ,快指针走过的距离为 ,其中 是快指针在环中绕了 圈。
由于快指针速度是慢指针的 倍,因此有 ,可以推出 ,即 。
也即是说,如果我们定义一个答案指针 指向链表头部,然后 和慢指针一起向前走,那么它们一定会在环入口相遇。
时间复杂度 ,其中 是链表中节点的数目。空间复杂度 。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast = slow = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
ans = head
while ans != slow:
ans = ans.next
slow = slow.next
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) for both hash table and two-pointer approaches, scanning each node at most twice. Space is O(n) for the hash table method and O(1) for two-pointer technique. Pointer resets and step calculations dominate runtime, especially when the cycle starts near the head. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate recognizes pointer manipulation and cycle detection pattern immediately.
- question_mark
Candidate distinguishes between detecting a cycle and locating its start.
- question_mark
Candidate considers edge cases with null or single-node lists and avoids modifying the original list.
常见陷阱
外企场景- error
Misidentifying the meeting point of fast and slow pointers as the cycle start.
- error
Failing to handle empty lists or single-node lists, causing null pointer errors.
- error
Using extra space unnecessarily when two-pointer solution suffices.
进阶变体
外企场景- arrow_right_alt
Return the cycle length instead of the starting node.
- arrow_right_alt
Detect multiple cycles in a list with added constraints.
- arrow_right_alt
Modify the list to mark visited nodes and detect the cycle, trading safety for space.