Linked List Cycle

🎯 Difficulty: EASY
🔗 LeetCode

Problem Statement

Given head, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. Otherwise, return false.

Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.

Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Approach: Fast and Slow Pointers (Floyd’s Cycle-Finding Algorithm)

The most optimal way to detect a cycle in a linked list is by using two pointers moving at different speeds. This is known as Floyd’s Cycle-Finding Algorithm (often referred to as the “Tortoise and Hare” algorithm).

  1. Initialize two pointers, slow and fast, both pointing to the head of the linked list.
  2. The slow pointer will move one step at a time (slow = slow.next).
  3. The fast pointer will move two steps at a time (fast = fast.next.next).
  4. We loop as long as fast and fast.next are not null. (If either becomes null, we’ve reached the end of the list, meaning there is no cycle).
  5. Inside the loop, after moving both pointers:
    • If slow and fast point to the exact same node (slow === fast), it means the fast pointer has lapped the slow pointer. This is only physically possible if there is a cycle. We return true.
  6. If the loop completes and we hit a null node, we return false.

Solution

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
function hasCycle(head) {
    let slow = head;
    let fast = head;
    
    // As long as fast and fast.next exist, we can safely move fast by 2 steps
    while (fast !== null && fast.next !== null) {
        slow = slow.next;         // Move slow by 1
        fast = fast.next.next;    // Move fast by 2
        
        // If they meet, there is a cycle
        if (slow === fast) {
            return true;
        }
    }
    
    // If we reach a null, there's an end to the list (no cycle)
    return false;
}

Complexity Analysis

  • Time Complexity: O(n)O(n) where nn is the number of nodes in the linked list. If there is no cycle, the fast pointer reaches the end in n/2n/2 steps. If there is a cycle, the fast pointer will catch up to the slow pointer in at most nn steps.
  • Space Complexity: O(1)O(1). We only use two extra pointers (slow and fast), so the memory footprint is constant.