1def detectCycle(head):
2 slow = fast = head
3
4 # Phase 1: Detect cycle
5 while fast and fast.next:
6 slow = slow.next
7 fast = fast.next.next
8
9 if slow == fast:
10 # Phase 2: Find entry point
11 slow = head
12 while slow != fast:
13 slow = slow.next
14 fast = fast.next
15 return slow
16
17 return None