AlgoMaster Logo

Middle of the Linked List

Last Updated: March 29, 2026

easy
3 min read

Understanding the Problem

At first, this seems straightforward: find the middle of a list and return it. But there are a couple of things worth thinking about.

First, in a singly linked list, we don't know the length upfront. There's no .length property or random access. We have to traverse node by node from the head. So finding the "middle" requires us to either count first or use a clever traversal trick.

Second, the even-length case matters. For a list with 6 nodes, there are two candidates for the middle: nodes at index 2 and index 3 (0-indexed). The problem says to return the second one, which is index 3. This detail affects where our pointer needs to land.

The key question is: can we find the middle in a single pass without knowing the length in advance?

Key Constraints:

  • Number of nodes in range [1, 100] → Very small input. Even O(n^2) would be instant. But the real goal is to learn the technique, which scales to any size.
  • 1 <= Node.val <= 100 → Values are positive integers. This doesn't affect the algorithm but simplifies debugging.
  • n >= 1 → The list is never empty. We don't need to handle the null head case.

Approach 1: Convert to Array

Intuition

The simplest way to deal with the "no random access" limitation of linked lists: just convert it to an array. Once we have an array, finding the middle is trivial. The middle index of an array of size n is n / 2 (integer division), and this conveniently gives us the second middle for even-length lists.

This approach uses extra space, but it makes the logic dead simple and is a good starting point.

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

This approach works but uses extra space. Can we find the middle without storing all nodes?

Approach 2: Two-Pass (Count then Traverse)

Intuition

If we don't want to use extra space, we can take two passes through the list. The first pass counts the total number of nodes. The second pass walks exactly n / 2 steps from the head to land on the middle node.

This eliminates the array entirely. We only need an integer counter and a pointer.

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

This works but requires two passes. What if two pointers could work together in a single pass to find the middle?

Approach 3: Slow and Fast Pointers

Intuition

This is the classic technique for finding the middle of a linked list in a single pass. The idea is simple but elegant: use two pointers, one moving at twice the speed of the other.

The slow pointer advances one node at a time. The fast pointer advances two nodes at a time. When fast reaches the end of the list (or goes past it), slow is exactly at 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
null
1/5

Code