📖 4 min read
Last updated on

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

SkillWhat Interviewers Observe
Fast-slow pointer fluencyDo you reach for this naturally or use two-pass?
Even-length list handlingDo you return the first or second middle? Can you explain why?
O(1) space constraintDo you avoid converting to an array?
Pattern recognitionDo you connect this to cycle detection and palindrome checking?

Step 1: Clarifying Questions

  1. For even-length lists, first or second middle? Problem says second — but confirm. This determines the termination condition.
  2. Is the list always non-empty? Assume yes per constraints.
  3. 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.

Linked list 1→2→3→4→5 with slow pointer at node 3 (middle) and fast pointer at node 5 (end). Node 3 is highlighted as the middle. ---

Step 4: Optimal Strategy

  1. Initialize slow = head, fast = head.
  2. While fast and fast.next:
    • slow = slow.next
    • fast = fast.next.next
  3. 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

AspectComplexity
TimeO(n)
SpaceO(1)

Common Interview Mistakes

  1. Wrong termination condition. while fast.next returns the first middle for even lists; while fast and fast.next returns the second. Know which you need.

  2. Null pointer on fast.next.next. The guard fast and fast.next ensures fast has both a value and a next node before advancing.

  3. 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.next stops 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.



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:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →