Skip to main content

19. Linked List Cycle

easyAsked at Plaid

Detect whether a linked list has a cycle. Plaid asks this as a baseline before harder graph-cycle problems on account-link chains and OAuth refresh-token graphs.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid backend OA.
  • LeetCode Discuss (2026)Plaid intro.

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

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; if a node is revisited, there's a cycle.

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

Tradeoff: Works but O(n) space. Mention as the obvious starting approach.

2. Floyd's tortoise and hare

Two pointers, slow steps by 1, fast steps by 2. If they meet, there's a 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 pointer-difference math is what makes them eventually meet inside any cycle.

Plaid-specific tips

Plaid grades this on whether you reach for Floyd over hash sets when O(1) space is required. Bonus signal: explain why fast moves 2 steps — any speed >= 2 would also work, but 2 is the smallest that guarantees they meet inside one revolution of the cycle.

Common mistakes

  • Checking fast.next.next without first checking fast.next — null deref.
  • Initializing slow = head and fast = head.next, then comparing — works but the symmetry is uglier.
  • Forgetting to handle the empty-list case (head is null).

Follow-up questions

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

  • Find the cycle start node (LC 142).
  • Find the cycle length.
  • Detect a cycle in a graph (OAuth token-refresh graph).

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 work?

Once both pointers are inside the cycle, the gap closes by 1 per iteration (fast gains 1 step). So they meet within at most one cycle length.

Why prefer Floyd over hash set?

Constant space. For Plaid's account-link graphs with millions of edges, allocating a hash set per check is wasteful.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →