Skip to main content
All questions
EasyLinked Lists · Fast and Slow Pointers

Linked List Cycle

Written by a community member · · 1 attempt · 100% pass

0

Given the head of a singly linked list, determine whether the list contains a cycle.

A cycle exists if, by repeatedly following the next pointer, you can reach a node that has already been visited. The input is provided to your function as only head; the cycle is constructed by the test harness. Do not modify the list.

Return True if the linked list contains a cycle, otherwise return False.

Constraints:

  • The number of nodes is between 0 and 10,000.
  • Node values are integers.
  • The list may contain duplicate values; cycle detection must be based on node identity, not values.
  • Aim for O(n) time and O(1) extra space.

Examples:

  1. Values [3, 2, 0, -4] with the tail connected to index 1 forms a cycle, so the result is True.
  2. Values [1, 2] with the tail connected to index 0 forms a cycle, so the result is True.
  3. Values [1] with no cycle returns False.