Middle of the Linked List (LeetCode 876): Solution and Explanation

Easy
Linked List
Two Pointers

Given the head of a singly linked list, return its middle node. When the list has an even number of nodes there are two middles, and you should return the second one.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

Without any tricks, you would walk the list once to count its nodes, then walk again count // 2 steps from the head. That is linear time and constant space, which is already optimal in big-O terms. Another option is to copy the nodes into an array and index the middle directly, which takes one pass but O(n) memory.

The more elegant idea finds the middle in a single pass with no counting. Imagine two runners starting at the head, one moving one node per step and the other moving two. When the fast runner reaches the end of the list, the slow runner has covered exactly half the distance, so it is standing on the middle node. This is the same slow and fast pointer technique used for cycle detection, applied to a list that is known to end.

The only detail to get right is the stopping condition, because it decides which middle you get for even lengths. The reference loop continues while both fast and fast.next exist. For a list of six nodes, the fast pointer goes from node 1 to 3 to 5 and then to None, which gives the slow pointer time to reach node 4, the second middle, as the problem requires. Stopping one step earlier, for example with fast.next and fast.next.next, would return the first middle instead.

Approach: Slow and fast pointers

  1. Point both slow and fast at head.
  2. While fast is not None and fast.next is not None, keep looping.
  3. In each iteration, move slow one node forward and fast two nodes forward.
  4. When the loop ends, fast is either on the last node (odd length) or past the end (even length).
  5. Return slow, which is the middle node, or the second of the two middles for even lengths.

Step-by-step example

head = [1, 2, 3, 4, 5, 6]
  1. Start with slow and fast on node 1.
  2. First iteration: slow moves to 2 and fast moves to 3.
  3. Second iteration: fast is on 3 and fast.next exists, so slow moves to 3 and fast moves to 5.
  4. Third iteration: fast.next is node 6, so the loop runs again. slow moves to 4 and fast jumps past 6 to None.
  5. The loop stops because fast is None, and the function returns node 4, the second middle. For [1, 2, 3, 4, 5] the loop would stop with fast on 5 and slow on 3.

Code: Python, Java, C++

1class Solution:
2    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
3        slow = fast = head
4        while fast and fast.next:
5            slow = slow.next
6            fast = fast.next.next
7        return slow

Time and space complexity

Time

O(n)

The fast pointer covers the list in about n / 2 iterations, and each iteration does constant work.

Space

O(1)

Only two pointers are stored, regardless of list length. The list itself is never modified or copied, which is why this version is preferred over collecting the nodes into an array.

Other approaches

Count then walk (time O(n), space O(1))

Traverse once to count the nodes, then traverse again count // 2 steps from the head. It is just as fast asymptotically and very easy to explain, but it takes two passes over the list.

Copy into an array (time O(n), space O(n))

Push every node into an array and return arr[len(arr) // 2]. It is a single pass with simple indexing, but it uses memory proportional to the list length.

Common mistakes

  • Using fast.next and fast.next.next as the loop condition. That returns the first middle for even-length lists, which is what Palindrome Linked List needs but not what this problem asks for.
  • Checking only fast.next without checking fast. On even-length lists fast becomes None and fast.next raises an error.
  • Moving fast twice without checking in between, for example in a language where you write fast = fast.next; fast = fast.next separately and forget the guard.
  • Returning slow.val instead of the node itself. The problem wants the node, so the caller receives the rest of the list from the middle onward.

Frequently asked questions

Why return the second middle node for even-length lists?

It is simply how the problem defines the middle. The fast and fast.next loop condition naturally lands on the second middle, so no special handling is needed.

How do I get the first middle instead?

Change the loop condition to fast.next and fast.next.next. The fast pointer then stops one step earlier on even lengths, leaving slow on the first middle.

Is the two-pointer method faster than counting?

Both are O(n). The two-pointer version does it in one traversal, which matters when the list is a stream you can only read once or when it is a building block inside a larger algorithm like merge sort on lists.