Skip to main content

19. Linked List Cycle

easyAsked at Vercel

Given a linked list, determine whether it has a cycle. Vercel asks this because Floyd's tortoise-and-hare is the gateway to a class of pointer-pacing tricks they use in their request-pipeline scheduler.

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel screen; Floyd's algorithm expected with no extra space.
  • LeetCode Discuss (2026-Q1)Listed in Vercel interview pool.

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
  • pos is -1 or a valid index in the linked-list.

Examples

Example 1

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

Explanation: The tail connects to node index 1.

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; add each node to a Set; if you ever see a node you've already added, there's a cycle.

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

Tradeoff: O(n) space; Vercel will ask you to do it in O(1).

2. Floyd's tortoise and hare (optimal)

Two pointers, slow advances by 1, fast by 2. If there's a cycle, fast eventually meets slow. If no cycle, fast reaches null.

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: O(1) extra space. The two-pointer pacing converges inside any cycle because fast gains 1 step per iteration on slow.

Vercel-specific tips

Vercel grades the Floyd's version. Bonus signal: explaining why fast gains 1 step per iteration on slow inside a cycle, and connecting to the follow-up question 'find the START of the cycle' (LC 142). Have both ready.

Common mistakes

  • Forgetting to check fast.next before fast.next.next — null pointer crash.
  • Starting slow at head.next and fast at head.next.next — works but the standard `both at head` is cleaner.
  • Returning false inside the loop on first non-match — they only need to NOT match this step; keep iterating.

Follow-up questions

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

  • Find the node where the cycle begins (LC 142).
  • Find the length of the cycle.
  • Remove the cycle and return the linear list.

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 fast advance by 2 catch slow inside a cycle?

Inside a cycle, fast gains exactly 1 step per iteration on slow. After at most cycle-length iterations, fast catches up. Outside any cycle, fast reaches null first and we return false.

Could fast skip over slow?

No. They're both on integer-indexed positions. If fast is one ahead of slow at iteration i, then at i+1 fast advances 2 and slow 1, so fast is 2 ahead — but wait, that means fast moves AROUND slow without landing on it. The cycle-meeting proof needs the modular reasoning: fast's index mod cycle_length will eventually equal slow's.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →