class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow, fast = head, head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
if slow == fast:
# cycle detected, restart
walker = head
while slow != walker:
walker = walker.next
slow = slow.next
return walker
return None
Alternative Solution
Find the length of the cycle
Move len(cycle) ahead from head
Iterate walker and this above pointer until they meet Details