Middle of the Linked List — Coding Interview Walkthrough
Middle of the Linked List is where most candidates first encounter the fast-slow pointer pattern. You can’t index into a linked list, so finding the middle requires a technique that feels counterintuitive until it clicks: move one pointer at double speed and let the other one land in the right place. This pattern is the foundation for cycle detection, palindrome checking, and merge sort on linked lists.
The Problem
Given the head of a singly linked list, return the middle node. If there are two middle nodes, return the second one.
Example 1
Input: [1,2,3,4,5]
Output: Node 3 (the middle)
Example 2
Input: [1,2,3,4,5,6]
Output: Node 4 (second of two middles)
When fast reaches the last node, slow is exactly at the middle. For even-length lists, slow lands on the second middle.
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Fast-slow pointer fluency | Do you reach for this naturally or use two-pass? |
| Even-length list handling | Do you return the first or second middle? Can you explain why? |
| O(1) space constraint | Do you avoid converting to an array? |
| Pattern recognition | Do you connect this to cycle detection and palindrome checking? |
Step 1: Clarifying Questions
- For even-length lists, first or second middle? Problem says second — but confirm. This determines the termination condition.
- Is the list always non-empty? Assume yes per constraints.
- Can I use O(n) extra space? The fast-slow approach avoids it, but worth asking to signal you know the trade-off.
Step 2: The Two-Pass Idea
Count the length in pass 1, advance n//2 steps in pass 2. O(n) time, O(1) space. Correct but requires two traversals.
Step 3: The Key Insight
Move fast pointer at 2x speed and slow pointer at 1x speed. When fast reaches the end, slow is at the middle. Single pass, O(1) space. The fast pointer’s end condition determines which middle you return for even-length lists.
---
Step 4: Optimal Strategy
- Initialize
slow = head,fast = head. - While
fast and fast.next:slow = slow.nextfast = fast.next.next
- Return
slow.
For even-length lists: fast reaches None (past last node), slow is at the second middle. ✓
Python Implementation
def middleNode(head: Optional[ListNode]) -> Optional[ListNode]:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
Trace for [1,2,3,4,5,6]:
- Start: slow=1, fast=1
- Step 1: slow=2, fast=3
- Step 2: slow=3, fast=5
- Step 3: slow=4, fast=None → exit
- Return node 4 ✓
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) |
Common Interview Mistakes
-
Wrong termination condition.
while fast.nextreturns the first middle for even lists;while fast and fast.nextreturns the second. Know which you need. -
Null pointer on
fast.next.next. The guardfast and fast.nextensures fast has both a value and a next node before advancing. -
Not knowing this is the foundation for harder problems. Interviewers often follow up with “how would you use this to sort a linked list?” (merge sort) or “detect a palindrome?” (fast-slow + reverse second half).
What a Strong Candidate Sounds Like
“I’ll use fast-slow pointers. Fast moves two nodes at a time, slow moves one. When fast reaches the end, slow is at the middle. For even-length lists, the condition
while fast and fast.nextstops when fast is None, which lands slow on the second middle. Single pass, O(1) space.”
Example Interview Dialogue
Interviewer: What if I wanted the first middle for even-length lists?
Candidate: I’d change the loop condition to while fast.next and fast.next.next. This makes fast stop one step earlier, so slow ends up at the first middle instead of the second.
Interviewer: Where else does this fast-slow pattern appear?
Candidate: Cycle detection — Floyd’s algorithm uses the same two-pointer approach. If there’s a cycle, fast and slow will eventually meet. It’s also used in merge sort on linked lists, where you find the middle to split the list in half.
Related Problems to Practice
- Linked List Cycle — Same fast-slow pattern for cycle detection.
- Reverse Linked List — Combine with middle-finding for palindrome checks.
- Merge Two Sorted Lists — Combine with middle-finding for merge sort on linked lists.
Practice This in a Mock Interview
The fast-slow pointer technique is one of those patterns that looks magical the first time but becomes second nature with practice. The key is articulating why the 2x speed invariant guarantees the slow pointer lands at the middle.
Start a mock interview for Middle of the Linked List on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub