141. Linked List Cycle

Description of the Problem

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.

Constraints:

  • The number of the nodes in the list is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • pos is -1 or a valid index in the linked-list.

Follow up: Can you solve it using O(1) (i.e. constant) memory?

Solution

Tags: LinkedList Fast-slow Pointers

Explanation

For any linkedlist (with or without a cycle), we can express as the following graph where \( k \ge 0\), \( \lambda \ge 0 \) and those \(\lambda\) nodes form a cycle.

Picture of LinkedList in general

Let \(n\) is the number of nodes in the linkedlist (i.e. \(n = k + \lambda \)).

We would like to show that if the linkedlist has a cycle, two pointers will meet in somewhere in those \(\lambda\) nodes (Proposition 1), and the time complxity is bounded by \(n\) (Proposition 2).

Also, let \(s^{(t)}\) be position of slow pointer after t iterations; let \(f^{(t)}\) be position of fast pointer after t iterations.

Proposition 1 - If the linkedlist has a cycle, two pointers will meet in somewhere the cycle:

Proof:

Consider the positions of two pointer in the cycle after k iterations: \(s^{(k)} = 0 \) and \(f^{(k)} = (k \mod \lambda) \)

They meet in somewhere in the cycle if and only if \(\exists t \gt 0 (s^{(k+t)} \equiv f^{(k+t)} \mod \lambda) \), evaluate the equation: \[ \begin{align} s^{(k+t)} & \equiv f^{(k+t)} & \mod \lambda \\ t & \equiv k+2t & \mod \lambda \\ 0 & \equiv k+t & \mod \lambda \\ t & \equiv -k & \mod \lambda \\ \end{align} \]

\( (t \equiv -k \mod \lambda) \) implies that t has a solution where \(0 \lt t \lt \lambda \).

Thus, Two pointer meet in some node in a cycle if the linkedlist has the cycle.

Proposition 2 - time complxity is bounded by \(n\):

Proof:

Consider a linkedlist without a cycle, the algorithm terminates in \(O(n)\).

Consider a linkedlist with a cycle, the algorithm runs less than \( k + \lambda = n \) iterations

Code (Java)

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(slow.next != null && fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                return true;
            }
        }

        return false;
    }
}

Complexity

  • n is number of nodes in a linkedlist

Time complexity:

  • \(T(n) = O(n)\)

Auxiliary Space:

  • \(S(n) = O(1)\)