AlgoMaster Logo

Middle of the Linked List

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

Finding the middle of a list sounds trivial, but two properties of a singly linked list complicate it.

First, we don't know the length upfront. There's no .length property and no random access. We can only traverse node by node from the head. Finding the middle therefore means either counting the nodes first or tracking position during a single traversal.

Second, the even-length case has two candidates. For a list with 6 nodes, the two middle positions are index 2 and index 3 (0-indexed). The problem asks for the second one, index 3. This determines exactly where the traversal must stop.

The question that drives the optimal solution: can we find the middle in a single pass, without knowing the length in advance?

Key Constraints:

  • Number of nodes in range [1, 100] → The input is small, so even an O(n) approach with extra space runs instantly. The technique below still matters because it scales to any list length.
  • n >= 1 → The list is never empty, so we don't need a special case for a null head. The first node always exists.

Approach 1: Convert to Array

Intuition

A linked list has no random access, but an array does. So copy every node reference into an array, then index straight to the middle. For an array of size n, the index n / 2 (integer division) is the middle. For even n it gives the higher of the two middle indices, which is the second middle node the problem asks for.

This costs O(n) extra space, but indexing into the array replaces any reasoning about where a traversal should stop.

Algorithm

  1. Traverse the linked list, storing each node in an array.
  2. Find the middle index: mid = array.length / 2.
  3. Return the node at array[mid].

Example Walkthrough

1Initialize: traverse list to collect nodes into array
1
current
2
3
4
5
null
1/5

Code

The array is the only reason this uses O(n) space. The next approach removes it by traversing the list twice instead of storing it.

Approach 2: Two-Pass (Count then Traverse)

Intuition

To avoid the array, traverse the list twice. The first pass counts the total number of nodes, n. The second pass walks exactly n / 2 steps from the head to reach the middle node.

This replaces the array with a single integer counter and a pointer, so the extra space drops to O(1).

Algorithm

  1. Traverse the entire list to count the total number of nodes n.
  2. Calculate the middle index: mid = n / 2.
  3. Traverse the list again from the head, advancing mid steps.
  4. Return the node we land on.

Example Walkthrough

1Pass 1: count nodes. current starts at head
1
current
2
3
4
5
null
1/6

Code

Two passes still traverse the list twice. The next approach reaches the middle in a single pass by moving two pointers at different speeds.

Approach 3: Slow and Fast Pointers

Intuition

Use two pointers that move at different speeds in a single pass. The slow pointer advances one node at a time. The fast pointer advances two nodes at a time. By the time fast reaches the end of the list (or moves past it), slow has reached the middle.

Algorithm

  1. Initialize slow = head and fast = head.
  2. While fast is not null and fast.next is not null, advance slow by one step and fast by two steps.
  3. When the loop ends, slow is the middle node. Return it.

Example Walkthrough

1Initialize: slow=node(1), fast=node(1)
slow
1
fast
2
3
4
5
6
null
1/6

Code