Finding the middle of a Linked List is one of the most important and frequently asked interview questions. It is also asked on LeetCode Problem 876 – Middle of the Linked List, making it highly relevant for coding interviews. While it sounds simple, the challenge is doing it efficiently in a single pass. If you’ve ever tried to find the middle of a race track while the race is still happening, you’ll love this logic!
To make this simple, we use a foolproof mental model: The Rabbit and The Tortoise.
The Core Logic: Fast & Slow Pointers
Imagine two racers starting at the beginning of the linked list:
-
The Tortoise (Slow): Moves one step at a time.
-
The Rabbit (Fast): Moves two steps at a time.
The Magic: Because the Rabbit moves twice as fast as the Tortoise, by the time the Rabbit reaches the finish line (the end of the list), the Tortoise will be exactly at the halfway mark!
Code Implementation
Here is how we translate that intuition into clean Python code using the Slow and Fast pointer approach.
def find_middle(head):
slow = head
fast = head
# While Rabbit can move two steps ahead...
while fast and fast.next:
# 1. Slow moves 1 step
slow = slow.next
# 2. Fast moves 2 steps
fast = fast.next.next
# When Rabbit hits the end, Slow is at the middle!
return slow
Let’s Understand the Code by Dry Run
Linked List: 1 → 2 → 3 → 4 → 5 → None
| Iteration | Slow Pointer (at node) | Fast Pointer (at node) | Explanation |
| Start | 1 | 1 | Both start at the head. |
| 1 | 2 | 3 | Slow +1, Fast +2. |
| 2 | 3 | 5 | Slow +1, Fast +2. |
| End | 3 | None | Fast cannot move further. |
Final Answer: The middle node is 3.
Still Confused? Watch the Full Explanation
I’ve explained this entire concept—from whiteboard logic to solving the LeetCode problem—on my YouTube channel as part of our Linked List Mastery series.
Get the Full Source Code
Finding the middle is just one part of the puzzle. You need the full Node class and LinkedList setup to test it. We have the complete, ready-to-run script available for you.
Access the Full Source Code on GitHub
Key Takeaways for Exams
-
Time Complexity: O(n), where n is the number of nodes. We traverse the list only once.
-
Space Complexity: O(1), as we only use two pointer variables (Slow and Fast).
-
Pro-Tip: For a list with an even number of nodes (e.g., 1 → 2 → 3 → 4), this logic returns the second middle node (3), which is exactly what LeetCode 876 requires!
Frequently Asked Questions (FAQs)
1. Why use two pointers instead of just counting the nodes first?
While you could count the total nodes ($n$) and then loop again to $n/2$, that requires two passes. The “Rabbit & Tortoise” method is optimized to finish the job in one pass.
2. Does this work for even-length lists?
Yes! If the list is 1 -> 2 -> 3 -> 4, the algorithm will return 3. If your specific requirement needs the first middle (2), you can slightly adjust the while loop condition.
3. What happens if the list is empty?
The code handles this gracefully. If head is None, both pointers stay None, and the function returns None without crashing.
4. Why is this so popular in interviews?
It tests your ability to think about efficiency. Interviewers want to see if you can move beyond “brute force” solutions to more elegant, mathematical approaches like pointer speed manipulation.
5. What is the next challenge?
Now that you can find the middle, the next logical step is learning how to detect a Cycle (Loop) in a Linked List. The “Rabbit & Tortoise” logic is also used there (Floyd’s Cycle-Finding Algorithm)!
What’s Next? Practice With Me!
You’ve mastered Middle of a Linked List. It’s time to level up!
Next Problem: [Detect a Cycle in a Linked List using Floyd’s Algorithm]
🎯 Build a Real Project (Don’t Stop at Theory!)
If you really want to master Linked Lists, don’t just solve problems — build something real.
👉 I’ve created a complete project using only:
- Singly Linked List
- Basic operations (insert, delete, reverse, traversal)
🔗 [Click here to explore the full project]
This will help you:
- Understand real-world use cases
- Strengthen DSA concepts
- Prepare for interviews + projects