Floyd 双阶段算法 — 检测环 & 找到入口 Medium #026
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