142. Linked List Cycle II

Floyd 双阶段算法 — 检测环 & 找到入口 Medium #026

阶段1: 检测环
相遇
阶段2: 找入口
结果
💡 点击示例或输入数据开始
0 / 0
📋 详细说明
等待开始…
✅ 结果
🐍 Python 代码
class Solution:
    def detectCycle(self, head):
        if not head or not head.next:
            return None
        slow = fast = head
        # Phase 1: detect cycle
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                # Phase 2: find entry
                ptr = head
                while ptr != slow:
                    ptr = ptr.next
                    slow = slow.next
                return ptr
        return None