Linked List Cycle - Leetcode Solution


đź’ˇ Step-by-Step Thought Process

  1. Understand the problem: Determine if a linked list has a cycle, where a cycle means a node’s next pointer points to a previously visited node.
  2. Initialize two pointers, slow and fast, both starting at the head of the list.
  3. While fast and fast.next are not None, move fast two steps (fast.next.next) and slow one step (slow.next).
  4. Check if slow equals fast (same node); if true, a cycle exists, so return True.
  5. If fast or fast.next becomes None, the list ends, so return False.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem: Linked List Cycle

The “Linked List Cycle” problem asks us to determine whether a given singly linked list contains a cycle. A cycle occurs when a node's next pointer points to a previously visited node, causing the list to loop indefinitely.

Example:

  • Input: A list where the last node links back to an earlier node
  • Output: true
  • Input: A list where the last node points to null
  • Output: false

Why This Problem Matters

Detecting cycles in a linked list is a foundational algorithm in computer science and has practical applications in memory management, graph traversal, and detecting infinite loops. This problem is also a great introduction to the two-pointer (tortoise and hare) technique.

Optimal Approach: Floyd’s Tortoise and Hare Algorithm

The most efficient method to detect a cycle in a linked list is to use two pointers:

  • Slow Pointer: Moves one node at a time.
  • Fast Pointer: Moves two nodes at a time.

If a cycle exists, the fast pointer will eventually “lap” the slow pointer — they will meet at some point. If there is no cycle, the fast pointer will reach the end of the list.

Steps:

  1. Initialize both slow and fast pointers at the head of the list.
  2. While fast and fast.next are not null:
    • Move slow one step forward (slow = slow.next).
    • Move fast two steps forward (fast = fast.next.next).
    • If slow === fast, a cycle exists — return true.
  3. If the loop ends without meeting, return false.

Example Walkthrough

Input: 3 → 2 → 0 → -4, with -4 pointing back to 2
Execution:

  • slow = 3, fast = 3
  • slow = 2, fast = 0
  • slow = 0, fast = 2
  • slow = -4, fast = -4 → match found → return true

Time and Space Complexity

Time Complexity: O(n), where n is the number of nodes. In the worst case, fast pointer visits each node at most twice.
Space Complexity: O(1), as no additional data structures are used.

Edge Cases to Consider

  • Empty list → return false
  • Single node with no cycle → return false
  • Single node that points to itself → return true

Conclusion

The “Linked List Cycle” problem is a classic use of the two-pointer technique. It shows how clever pointer manipulation can solve problems efficiently without the need for additional memory. Mastering this approach will prepare you for more advanced problems involving cycles and fast-slow pointer techniques in graphs and linked lists.

Get Personalized Support at AlgoMap Bootcamp đź’ˇ