const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Linked List Cycle LeetCode Solution: Complete Walkthrough

Linked List Cycle (#141) introduces Floyd's Tortoise and Hare algorithm — the O(1) space cycle detection technique that is both elegant and interview-essential.

8 min read|

Floyd's Tortoise and Hare: O(1) space cycle detection

The elegant two-pointer technique behind Linked List Cycle (#141)

Linked List Cycle LeetCode: Why This Problem Matters

Linked List Cycle (#141) is one of those problems that every serious interview candidate encounters. It is a gateway to one of the most elegant algorithms in computer science: Floyd's Tortoise and Hare cycle detection. Once you understand it, you unlock a pattern that applies far beyond linked lists.

The linked list cycle LeetCode problem asks a deceptively simple question — does this linked list contain a cycle? But the real challenge is answering it in O(1) space, which is exactly what interviewers want to see. This walkthrough covers both the intuitive hash set approach and the optimal Floyd's algorithm.

Whether you are preparing for FAANG interviews or building your fundamentals, this problem teaches you the fast slow pointer pattern that appears in dozens of other problems. Let's break it down step by step.

Understanding the Linked List Cycle Problem

Given the head of a linked list, determine if the list has a cycle. A cycle exists when some node's next pointer connects back to a previously visited node, creating an infinite loop. If you tried to traverse to the end, you would never reach null.

The input includes a pos parameter (used only for test case generation) indicating which node the tail connects to. Your function receives only the head node — you do not have access to pos. You must return true if a cycle exists, false otherwise.

This problem is classified as Easy on LeetCode, but it introduces concepts that are foundational for Medium and Hard linked list problems. The follow-up question — detecting where the cycle starts — is LeetCode #142, a Medium problem that builds directly on this one.

  • Input: Head of a singly linked list
  • Output: Boolean — true if a cycle exists, false otherwise
  • Constraint: Can you solve it with O(1) memory?
  • A cycle means traversal never reaches null — it loops forever
ℹ️

Historical Note

Floyd's cycle detection algorithm was published in 1967 and remains the optimal solution — no algorithm can detect cycles in O(n) time with less than O(1) extra space.

Approach 1: Hash Set — Detect Cycle with Extra Space

The most intuitive approach to detect a cycle in a linked list is using a hash set. As you traverse the list, store each visited node in the set. If you encounter a node that is already in the set, you have found a cycle. If you reach null, the list is cycle-free.

This approach works perfectly and is easy to implement. You simply iterate through the list, checking membership in the set at each step. The moment you see a duplicate, return true. If the traversal ends at null, return false.

The time complexity is O(n) since you visit each node at most once. The space complexity is also O(n) because in the worst case you store every node in the set before detecting the cycle or reaching the end. This is where the follow-up challenge comes in — can you do better on space?

  • Create an empty hash set to track visited nodes
  • Traverse the list from head, adding each node to the set
  • If current node is already in the set, return true (cycle found)
  • If you reach null, return false (no cycle)
  • Time: O(n) — visit each node once. Space: O(n) — store all nodes

Approach 2: Floyd's Tortoise and Hare — Linked List Cycle in O(1) Space

Floyd's cycle detection algorithm is the optimal solution for the linked list cycle LeetCode problem. The idea is beautifully simple: use two pointers moving at different speeds. The slow pointer (tortoise) moves one step at a time, while the fast pointer (hare) moves two steps at a time.

If there is no cycle, the fast pointer will reach the end of the list (null) and you return false. If there is a cycle, the fast pointer will eventually lap the slow pointer — they will meet at the same node. When they meet, you return true.

The implementation is remarkably concise. Initialize both pointers at the head. In each iteration, advance slow by one node and fast by two nodes. Check if they point to the same node. That is the entire algorithm — four lines of logic that solve the problem optimally.

The time complexity is O(n). In the worst case, the slow pointer traverses the entire list. The space complexity is O(1) — you only use two pointer variables regardless of the list size. This is what makes Floyd's algorithm superior to the hash set approach.

  1. 1Initialize slow and fast pointers both at head
  2. 2While fast and fast.next are not null, advance slow by 1 step and fast by 2 steps
  3. 3If slow equals fast at any point, return true — a cycle exists
  4. 4If the loop exits (fast reached null), return false — no cycle
💡

Pro Tip

Floyd's algorithm is just 4 lines of code: initialize slow and fast to head, advance slow by 1 and fast by 2, if they meet there's a cycle. The simplicity is the beauty.

Why Floyd's Cycle Detection Algorithm Works

The mathematical intuition behind Floyd's algorithm is elegant. If there is a cycle, once both pointers enter the cycle, the fast pointer gains exactly one step on the slow pointer per iteration. Think of it like two runners on a circular track — the faster runner will inevitably lap the slower one.

Let's think about it more precisely. Once both pointers are inside the cycle, the distance between them decreases by one with each iteration. Since the fast pointer gains one position per step, the gap shrinks from whatever it was down to zero. They must meet within at most one full traversal of the cycle.

If there is no cycle, the fast pointer simply races ahead and hits null. It can never "lap" the slow pointer because there is no loop to circle back through. This is why the null check on fast and fast.next is sufficient — it guarantees termination in the acyclic case.

This proof extends to any cycle length and any tail length before the cycle. The relative speed difference of 1 ensures convergence regardless of the specific structure. It is one of those rare algorithms where the proof is as clean as the code.

Edge Cases and Follow-Up: Linked List Cycle II

Before submitting, consider the edge cases. An empty list (head is null) has no cycle — return false immediately. A single node with no self-loop also has no cycle. A single node whose next points to itself is the simplest possible cycle. Your implementation handles all of these if you check fast and fast.next correctly.

The natural follow-up is LeetCode #142, Linked List Cycle II, which asks you to find the node where the cycle begins. This requires Floyd's "phase 2": after the slow and fast pointers meet inside the cycle, reset one pointer to the head. Then advance both pointers one step at a time. The node where they meet again is the start of the cycle.

This phase 2 trick works because of a mathematical property: the distance from the head to the cycle start equals the distance from the meeting point to the cycle start (traveling around the cycle). Understanding this elevates your grasp of pointer manipulation from practical to theoretical.

  • Empty list (null head): Return false immediately
  • Single node, no self-loop: Return false
  • Single node pointing to itself: Return true (simplest cycle)
  • Cycle II (#142): Find the cycle START node, not just detect existence
  • Floyd's phase 2: Reset one pointer to head, advance both by 1 until they meet at cycle start
⚠️

Watch Out

Linked List Cycle II (#142) asks you to find the cycle START, not just detect it — this requires Floyd's 'phase 2': after detection, reset one pointer to head and advance both by 1 until they meet.

What Linked List Cycle Teaches You

Linked List Cycle (#141) is a gateway to the fast/slow pointer pattern, which is one of the most versatile techniques in algorithm interviews. The same two-pointer approach helps you find the middle of a linked list (#876), detect palindrome linked lists (#234), and even solve the Happy Number problem (#202) — which has nothing to do with linked lists at all.

The deeper lesson is that pointer manipulation problems reward understanding over memorization. If you understand why Floyd's algorithm works, you can derive it on the spot. If you just memorize the code, you will struggle when the interviewer tweaks the problem. This is exactly the kind of pattern recognition that YeetCode flashcards are designed to reinforce.

Add Linked List Cycle to your review rotation. Practice recognizing when a problem involves cycle detection or the fast/slow pointer pattern. Once this technique becomes second nature, a whole category of linked list and mathematical problems opens up to you.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now