Linked List Cycle II

Problem Summary

What is Being Asked?

Key Observations

Approach Taken

Main Concepts Used

Time & Space Complexity

Code

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