In our last lesson, we established the fundamental operations for singly linked lists, focusing on the careful pointer manipulation required for insertion, deletion, and reversal. You practiced how to rearrange next pointers, a skill that is the bedrock for more advanced linked list algorithms.
Today, we'll build directly on that foundation by exploring a powerful problem-solving pattern: the fast and slow pointer technique. Also known as the "tortoise and hare" algorithm, this approach allows you to solve certain linked list problems in a single pass with constant extra space—a significant improvement over more intuitive but less efficient methods. Specifically, we will apply this technique to two classic interview problems: finding the middle element of a linked list and detecting if a list contains a cycle.
The Fast and Slow Pointer Pattern
The core idea is simple yet elegant. You initialize two pointers, typically named slow and fast, at the head of the linked list. Then, you traverse the list by moving the pointers at different speeds:
- The
slowpointer advances one node at a time:slow = slow.next. - The
fastpointer advances two nodes at a time:fast = fast.next.next.
This difference in speed creates a dynamic relationship between the pointers that we can exploit to gather information about the list's structure.
Fast and Slow Pointers: A Powerful Technique for Solving ...
This article from Algocademy provides a concise introduction to the technique and its common applications.
Start by reading the section "Understanding Fast and Slow Pointers" to grasp the main concept. Next, read the section "How Fast and Slow Pointers Work" and review the basic Python implementation. Finally, glance over the list of common applications to see the breadth of problems this single pattern can solve.
Now, let's put this pattern into practice with our first problem.
Application 1: Finding the Middle of a Linked List
Problem: Given the head of a singly linked list, return the middle node of the list. If the list has an even number of nodes, there are two middle nodes; in this case, return the second one.
A naive approach would be to traverse the list once to count the total number of nodes, N, then traverse it a second time up to N/2 to find the middle node. This works, but it requires two full or partial passes. The fast and slow pointer technique allows us to do it in just one.
The intuition is straightforward: since the fast pointer moves twice as fast as the slow pointer, by the time fast reaches the end of the list, slow will naturally be positioned at the middle.
The following video provides an excellent walkthrough of this logic.
Master the Fast & Slow Pointer Coding Pattern for FAANG Interviews! Boost Your Coding Skills
This video from Destination FAANG explains how to use the fast and slow pointer technique to find the middle of a linked list. The presenter compares it to the less efficient two-pass method, which is a great way to frame the solution in an interview.
Watch the segment from the explanation of finding the middle element. Pay close attention to the while loop condition (while fast and fast.next) and how it elegantly handles both odd and even length lists.
As the video demonstrated, the loop termination condition is key:
- Odd-length list: The
fastpointer will end up on the very last node.fast.nextwill beNone, terminating the loop. - Even-length list: The
fastpointer will land on the second-to-last node, and its next jump (fast.next.next) will make itNone, terminating the loop.
In both scenarios, the slow pointer will be left at the correct middle node.

Here is a typical Python implementation:
def find_middle(head):
slow = head
fast = head
# The loop continues as long as fast and the node after it are not None.
# This prevents an error when trying to access fast.next.next.
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# When the loop ends, slow is at the middle.
return slow
This single-pass solution has a time complexity of and, crucially, a space complexity of , making it highly efficient.
Application 2: Detecting a Cycle (Floyd's Tortoise and Hare)
Problem: Given the head of a linked list, determine if the list has a cycle in it.
This is a classic interview question. How can you tell if a list loops back on itself? If you simply traverse it, you'll get stuck in an infinite loop.
A good first thought, which you might propose in an interview, is to use a hash set to keep track of visited nodes.
Linked List Cycle - Floyd's Tortoise and Hare - Leetcode 141 - Python
This NeetCode video starts by explaining the hash set approach for cycle detection. This is an important stepping stone to understanding the value of the optimized solution.
Watch the initial part of the video from the explanation of the hash set method. Note the time and space complexity trade-off: O(N) time and O(N) space.
The space complexity of the hash set method is often a point an interviewer will press on, asking for a more memory-efficient solution. This is where the fast and slow pointer technique shines, providing an space solution.
The logic is as follows:
- If there is no cycle, the
fastpointer will eventually reach the end of the list (None). - If there is a cycle, the
fastpointer will enter the cycle and start "lapping" theslowpointer. Because thefastpointer gains one position on theslowpointer with every iteration inside the cycle, they are guaranteed to meet eventually.
The next segment of the NeetCode video gives a brilliant, intuitive proof for why they are guaranteed to meet.
Linked List Cycle - Floyd's Tortoise and Hare - Leetcode 141 - Python
Continuing with the NeetCode video, the presenter now introduces the O(1) space solution and explains precisely why it works.
First, watch the simulation of the pointers from this section. Then, pay close attention to the explanation of "gap reduction" from this proof. Understanding that the gap between the pointers closes by exactly one node per iteration is the key insight.
With that intuition, the implementation is surprisingly concise.
Linked List Cycle - Floyd's Tortoise and Hare - Leetcode 141 - Python
Finally, let's look at the code implementation.
Watch the coding part of the video from the code walkthrough. The structure is very similar to our middle-finding code, but the logic inside the loop is different. We check for a meeting of the pointers (slow == fast).
Follow-up: Finding the Start of the Cycle
A common follow-up question is: "Great, you've detected a cycle. Now can you find the node where the cycle begins?"
Floyd's algorithm includes a beautiful second phase for this. The proof is based on the distances traveled by the pointers, but the algorithm itself is simple:
- Use the fast and slow pointers to find the first meeting point within the cycle.
- Once they meet, reset one pointer (say,
slow) back to theheadof the list. Leave the other pointer (fast) at the meeting point. - Now, advance both pointers one step at a time (
slow = slow.next,fast = fast.next). - The node where they meet again is the start of the cycle.
This two-phase process is perfectly illustrated in the following diagram.

The code for this extension builds directly on the detection code.
Fast and Slow Pointers: A Powerful Technique for Solving ...
The Algocademy article we looked at earlier provides a clean implementation for this follow-up problem.
Review the code in the section "Detect the start of a cycle". Notice how it's structured in two parts: first, find the meeting point, and second, find the cycle's start.
Conclusion
In this lesson, you've added a powerful and efficient pattern to your toolkit. The fast and slow pointer technique is a prime example of the kind of algorithmic thinking that interviewers at top companies look for: identifying an elegant solution that optimizes for space and time.
Key Takeaways:
- The fast and slow pointer technique uses two pointers moving at different speeds to solve problems on sequences, particularly linked lists.
- It provides an space complexity solution for problems that might otherwise require space (like using a hash set).
- Finding the middle element: When the
fastpointer reaches the end of the list, theslowpointer is at the middle. - Cycle detection: If the
fastandslowpointers ever meet, a cycle exists. They are guaranteed to meet if a cycle is present. - Finding the cycle's start: After finding a meeting point, reset one pointer to the head and advance both one step at a time. Their next meeting point is the start of the cycle.
In our next lesson, we will tackle another classic linked list problem: merging two sorted lists. This will further hone your pointer manipulation skills and introduce a core component used in famous sorting algorithms.