Skip to main content

19. Linked List Cycle

easyAsked at Salesforce

Detect whether a linked list contains a cycle. Salesforce asks this to test the Floyd's tortoise-and-hare algorithm — a foundational two-pointer trick used in their dependency-cycle detection for workflow rules.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Workflow-rule cycle detection uses this exact pattern in production.
  • Blind (2025-11)Asked on Salesforce backend onsites as a fundamentals check.

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, 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: There is a cycle where the 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. Visited set

Walk the list, store each node in a set; if you revisit, there's a cycle.

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: O(n) space. Salesforce will push you to O(1).

2. Floyd's tortoise and hare

Two pointers, slow moves 1 step, fast moves 2. If they meet, there's a cycle. 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: O(1) space. The fast pointer 'laps' the slow one inside any cycle. Termination relies on fast hitting null in acyclic lists.

Salesforce-specific tips

Salesforce uses cycle detection in workflow-rule dependency graphs (a rule can't recurse on itself or a peer). They grade on whether you check BOTH fast && fast.next in the loop condition — forgetting one crashes on the boundary. Bonus signal: extend to Linked List Cycle II (LC 142) which returns the cycle start using the same algorithm with a second pass.

Common mistakes

  • Checking slow === fast at the top of the loop (before advancing) — returns true on the very first iteration.
  • Only checking fast (not fast.next) — null-pointer crash on the next-next access.
  • Trying to use the value-based equality — values can repeat in a valid acyclic list.

Follow-up questions

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

  • Linked List Cycle II (LC 142) — find the start node of the cycle.
  • Happy Number (LC 202) — same algorithm, different domain.
  • Detect cycle and return cycle length.

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 the fast/slow trick work?

Inside a cycle of length k, fast gains 1 position on slow every iteration. After at most k iterations they meet. Outside a cycle, fast hits null first.

What if I use 3x speed for fast?

Still works for detection but the meeting point is no longer convenient for LC 142. Stick with 2x — it's the canonical version.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →