Hello! Welcome back to our module on "High-ROI Interview Patterns."
In the last lesson, we focused on the converging two-pointer technique for arrays and strings. You learned how to efficiently solve problems like "Two Sum on a Sorted Array" and "Valid Palindrome" in linear time and constant space by moving pointers from opposite ends toward the middle.
Today, we're exploring another powerful variation of this pattern: the fast and slow pointer technique. This approach, often called the "Hare and Tortoise" algorithm, is a cornerstone for solving linked list problems. Our goal is to apply the fast and slow pointer technique to detect cycles and find the middle of a linked list. Mastering this will give you a significant edge, as it provides an optimal O(1) space solution to problems that might otherwise seem to require extra memory.
1. The Core Concept: Hare and Tortoise
The idea is simple yet brilliant. We use two pointers, but instead of moving them towards each other, they both start at the beginning of the data structure and move in the same direction, but at different speeds.
- A slow pointer moves one step at a time (
slow = slow.next). - A fast pointer moves two steps at a time (
fast = fast.next.next).
This relative speed difference is the key to unlocking solutions for several tricky linked list problems. Let's start with a short video that introduces the fundamental concept.
Master the Fast & Slow Pointer Coding Pattern for FAANG Interviews! Boost Your Coding Skills
The video 'Master the Fast & Slow Pointer Coding Pattern' from Destination FAANG provides an excellent introduction to the core idea behind this pattern.
Watch from the beginning to 03:39. Focus on how the pointers move at different speeds and the two primary use cases mentioned: finding the middle element and detecting cycles.
2. Application 1: Finding the Middle of a Linked List
A common interview question is to find the middle node of a singly linked list in a single pass.
The Problem: Given the head of a singly linked list, return the middle node. If there are two middle nodes (in an even-length list), return the second one.
A naive approach would be to traverse the list once to find its length (n), then traverse it a second time up to n/2 to find the middle node. This works, but it requires two passes. The fast and slow pointer technique elegantly solves this in a single pass.
The Logic:
By the time the fast pointer reaches the end of the list, the slow pointer will be exactly at the midpoint. Think about it: the slow pointer travels half the distance of the fast pointer.

For a detailed walkthrough of this logic, including how it handles both odd and even length lists and a code implementation, let's return to the Destination FAANG video.
Master the Fast & Slow Pointer Coding Pattern for FAANG Interviews! Boost Your Coding Skills
This segment explains the 'Middle of the Linked List' problem in detail, contrasting the two-pass approach with the optimal one-pass fast and slow pointer solution.
Watch the section from 03:39 to 08:45. Pay close attention to the loop termination condition (while fast and fast.next) and how it ensures the slow pointer ends up at the correct middle node.
The Python implementation is concise and demonstrates the pattern's elegance:
def middleNode(self, head):
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
3. Application 2: Detecting a Cycle in a Linked List
This is the classic problem for which this pattern was originally popularized, known as Floyd's Cycle-Finding Algorithm.
The Problem: Given the head of a linked list, determine if it has a cycle. A cycle exists if some node in the list can be reached again by continuously following the next pointer.
A common first thought is to use a hash set to store each node as you traverse the list. If you encounter a node that's already in the set, you've found a cycle. This works, but your interviewer will almost certainly follow up with: "Can you do it in O(1) space?"
This is where the fast and slow pointers shine.
The Logic:
- Initialize
slowandfastpointers at the head. - Move them at their respective speeds.
- If the linked list has a cycle, the
fastpointer will enter the cycle first, followed by theslowpointer. Eventually, thefastpointer will "lap" theslowpointer, and they will meet at the same node. - If the linked list does not have a cycle, the
fastpointer (orfast.next) will reachnull.

Why is a meeting guaranteed?
It's crucial to understand why they are guaranteed to meet. Once both pointers are inside the cycle, the fast pointer gains one position on the slow pointer with every iteration. Imagine two runners on a circular track; the faster runner will inevitably lap the slower one.
For a rigorous but intuitive explanation of this, the NeetCode channel provides one of the best breakdowns.
Linked List Cycle - Floyd's Tortoise and Hare - Leetcode 141 - Python
The video 'Linked List Cycle' by NeetCode provides a fantastic mathematical intuition for why the pointers are guaranteed to meet inside a cycle.
Watch from 03:16 to 09:10. The first part (until 04:55) introduces the algorithm visually. The second part (from 05:50) explains mathematically why the gap between the pointers closes by exactly one node per iteration within the cycle, guaranteeing they will meet.
Implementation
Now that you understand the what and the why, let's look at a full walkthrough that contrasts the hash set approach with the optimal one and provides a clean implementation.
Fast and Slow Pointers (Java, Python, and JavaScript)
The article 'Fast and Slow Pointers' provides a clear, written explanation and code for the cycle detection problem.
Read the section titled '1: Linked List Cycle (Leetcode 141)'. Compare the 'Simple Approach' (using a HashSet) with the 'Optimized Approach: Fast and Slow Pointers'. Note the O(n) space vs. O(1) space tradeoff, which is a key discussion point in interviews.
Test your understanding!
Consider a singly linked list with 10 nodes (numbered 1 to 10). The next pointer of node 10 points back to node 4, creating a cycle.
If you start slow and fast pointers at node 1, what is the first node inside the cycle that the slow pointer will visit?
Show answer
The slow pointer moves one step at a time. It will visit nodes 1, 2, and 3 outside the cycle. Its next step will be to node 4, which is the start of the cycle.
Therefore, the first node inside the cycle that the slow pointer visits is node 4.
Conclusion
You've now added a second major two-pointer pattern to your toolkit. While the converging pointer pattern is your go-to for sorted arrays, the fast and slow pointer pattern is the key to optimally solving many linked list problems.
Key Takeaways:
- The Fast and Slow Pointer (Hare and Tortoise) technique uses two pointers moving at different speeds (usually 1x and 2x) through a sequence.
- It finds the middle of a linked list in a single pass, as the slow pointer will be at the midpoint when the fast pointer reaches the end.
- It detects cycles in O(1) space by guaranteeing that the fast pointer will eventually lap and meet the slow pointer if a cycle exists. This O(1) space complexity is its main advantage over a hash set solution.
The article Fast & Slow Pointers: Master Floyd's Cycle Detection provides a concise summary and some excellent interview tips in the section "Interview Tips & Tricks" (resource ID LINK, section 5) which are worth reviewing as you practice.
In our next lesson, we will continue building our linked list manipulation skills by tackling another fundamental interview problem: reversing a linked list iteratively using pointers.