20. Linked List Cycle
easyAsked at WorkdayDetermine if a linked list has a cycle. Workday uses this for Floyd's tortoise-and-hare fluency — same pattern that catches circular reporting in org-chart imports.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Workday loops.
- Glassdoor (2025)— Workday SDE1 phone screen.
- Blind (2026)— Workday org-chart team specifically cites the circular-reporting analogy.
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^5pos is -1 or a valid index in the linked-list.
Examples
Example 1
head = [3,2,0,-4], pos = 1trueExample 2
head = [1,2], pos = 0trueExample 3
head = [1], pos = -1falseApproaches
1. Hash set of visited nodes
Walk the list, hash each node. If we see one twice, cycle.
- Time
- O(n)
- Space
- O(n)
const seen = new Set();
let n = head;
while (n) { if (seen.has(n)) return true; seen.add(n); n = n.next; }
return false;Tradeoff: O(n) extra space. Works but the interviewer wants O(1).
2. Floyd's tortoise and hare
Slow pointer moves 1 step, fast 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: O(1) extra space. The proof: if a cycle exists, the fast pointer eventually laps the slow inside the cycle.
Workday-specific tips
Workday wants the O(1)-space solution. Mention Floyd's by name. The loop condition `fast && fast.next` is what protects against null dereference — get this right out loud before coding.
Common mistakes
- Checking slow === fast BEFORE advancing them — they start equal, so you'd return true immediately on any list.
- Loop condition missing `fast.next` — fast.next.next crashes.
- Advancing only one step at a time for both — they'd never meet.
Follow-up questions
An interviewer at Workday may pivot to one of these next:
- Linked List Cycle II (LC 142) — find the cycle entry point.
- Happy Number (LC 202) — Floyd's on a non-linked-list graph.
- Find the Duplicate Number (LC 287).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does the hare move twice as fast?
Inside the cycle, the hare gains one step per iteration. If the cycle has length k, it laps the tortoise within k steps.
Why check fast.next as well as fast?
Because fast.next.next requires fast.next to exist. Skipping the inner check crashes on lists with no cycle.
Practice these live with InterviewChamp.AI
Drill Linked List Cycle and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →