Skip to main content

20. Linked List Cycle

easyAsked at Reddit

Detect whether a linked list has a cycle. Reddit uses Floyd's cycle detection (tortoise and hare) to test pointer fluency — the same algorithm used in their comment-graph integrity checker to detect malformed reply cycles.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit infra-team phone screen.
  • Blind (2025-11)Reported as a Reddit warm-up before harder graph-integrity questions.

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. Return true if there is a cycle in the linked list. Otherwise, return false.

Constraints

  • The number of the nodes in the list is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5

Examples

Example 1

Input
head = [3,2,0,-4], pos = 1
Output
true

Explanation: There is a cycle where tail connects to the 1st node (0-indexed).

Example 2

Input
head = [1,2], pos = 0
Output
true

Example 3

Input
head = [1], pos = -1
Output
false

Approaches

1. Hash set of visited nodes

Walk the list, store each node in a set. If you ever see one again, cycle exists.

Time
O(n)
Space
O(n)
function hasCycle(head) {
  const seen = new Set();
  let cur = head;
  while (cur) {
    if (seen.has(cur)) return true;
    seen.add(cur);
    cur = cur.next;
  }
  return false;
}

Tradeoff: Linear but O(n) extra space. Acceptable but the follow-up always asks for O(1).

2. Floyd's tortoise and hare (optimal)

Slow pointer moves 1, fast pointer moves 2. If they meet, cycle exists. If fast hits null, no cycle.

Time
O(n)
Space
O(1)
function hasCycle(head) {
  let slow = head, fast = head;
  while (fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) return true;
  }
  return false;
}

Tradeoff: Constant space. The 'two-pointer at different speeds' is canonical.

Reddit-specific tips

Reddit interviewers always follow up with 'and where does the cycle begin?' (LC 142). Be ready with the two-phase Floyd's algorithm. Bonus signal: connect to their comment-graph integrity audit — they validate that reply-pointer chains don't cycle.

Common mistakes

  • Checking fast === slow before advancing — they start equal so it always returns true.
  • Forgetting fast.next check before fast.next.next — crashes on non-cycle lists.
  • Comparing values instead of node references — false positives on duplicate values.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Linked List Cycle II (LC 142): return the node where the cycle begins.
  • Happy number (LC 202) — same algorithm on a derived sequence.
  • Find the duplicate number (LC 287).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does Floyd's algorithm work?

If there's a cycle, fast laps slow inside it. Each iteration the gap between them shrinks by 1 (modulo cycle length), guaranteeing they meet.

When would the hash set version be preferred?

When you also need the offending node — Floyd's only tells you 'cycle exists.' Phase 2 of Floyd's finds the entry node, but hash gives it directly.

Practice these live with InterviewChamp.AI

Drill Linked List Cycle and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →