Linked List Cycle

Problem Summary

Key Observations

Why This Works

(Explain the core reason the solution is correct.)

Example Scenario

Imagine two friends walking around a circular track:

  • One friend (the “slow” pointer) walks one lap per hour.
  • Another friend (the “fast” pointer) runs two laps per hour. If there is a cycle (the track is circular), the fast pointer will eventually “lap” or catch up to the slow pointer. This concept directly translates to data structures where pointers traverse nodes.

Main Concepts Used

(Mark the CS concepts or algorithms used.)

Time & Space Complexity

Code

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        slow, fast = head, head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
            if slow == fast:
                return True
        return False

Common Mistakes / Things I Got Stuck On