LeetCode #876 Easy

Middle of the Linked List

Middle of the Linked List is LeetCode 876 (Easy). Given the head of a singly linked list, return its middle node.

  • If the list has an even number of nodes, there are two middle nodes; return the second one.
  • The list is never empty.
Constraints
  • The number of nodes in the list is in the range [1, 100].
  • 1 <= Node.val <= 100
linked-listtwo-pointers
Open on LeetCode ↗
02

Intuition

You could count the nodes and then walk half way, but two pointers find the middle in a single pass. Move slow one node and fast two nodes per round; when fast cannot jump again, slow is in the middle.

After k rounds slow is k hops from the head and fast is 2k hops. The loop stops at the first round where fast is on the last node (2k = n - 1) or has stepped past it (2k = n). Either way k is n // 2, and the node n // 2 hops from the head is the middle, the second one when n is even.

How to spot this pattern

When you need a position defined by the length of a linked list (the middle, a third of the way, a cycle's entry) but cannot index into it, run two pointers at different speeds. Their gap encodes the position without a counting pass.

03

Approach

Try it first

Before reading on, try two pointers that move at different speeds and work out by hand where the slow one ends on lists of 5 and 6 nodes. Then decide exactly when the fast one must stop. Aim for one pass and O(1) space.

1

Start both pointers at the head

Set slow = fast = head. Both have travelled 0 hops, so fast's distance is double slow's from the very start.

2

Move one hop and two hops per round

Each round, set slow = slow.next and fast = fast.next.next. The double hop keeps fast's distance at exactly twice slow's after every round.

3

Stop when fast cannot jump twice

Loop while fast and fast.next both exist. If either is missing, a double hop would leave the list, and fast has covered the whole list, so slow has covered half of it.

4

Return slow

Return slow. On an odd length it is the single middle; on an even length fast ends on None, one hop past the last node, which puts slow on the second middle as required.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def middleNode(self, head):
▶3 slow = fast = head
▶4 while fast and fast.next:
▶5 slow = slow.next
▶6 fast = fast.next.next
▶7 return slow
12345nullslowfastslow: 0 hopsfast: 0 hopsslow and fast both start at the head
rounds0
slownode 10 hops
fastnode 10 hops = 2 × 0
Both pointers start on the first node. Each round, slow moves one node and fast moves two, so fast's distance is always exactly double slow's.
12345nullslowfastslow: 1 hopfast: 2 hopsround 1: slow on node 2, fast on node 3
rounds1
slownode 21 hops
fastnode 32 hops = 2 × 1
Before moving, fast stood on a node that had a next node, so the two-node jump was safe. Now slow has gone 1 hop and fast 2, still double.
12345nullslowfastslow: 2 hopsfast: 4 hopsround 2: slow on node 3, fast on node 5
rounds2
slownode 32 hops
fastnode 54 hops = 2 × 2
Before moving, fast stood on a node that had a next node, so the two-node jump was safe. Now slow has gone 2 hops and fast 4, still double.
12345nullslowfastslow: 2 hopsfast: 4 hopsfast is on the last node → stop
rounds2
slownode 32 hops
fastnode 54 hops = 2 × 2
fast has no next node, so another double jump is impossible and the loop ends. fast covered 4 hops, the whole list, so slow at half of that, 2, is the exact middle.
12345nullmiddleslow: 2 hopsfast: 4 hopsanswer: node 3 (value 3)
rounds2
slownode 32 hops
fastnode 54 hops = 2 × 2
Return slow. The returned list starts at node 3 of 5, the single middle.
05

Common pitfalls

Checking only fast.next

✗ Wrong
while fast.next:
✓ Right
while fast and fast.next:

On [1, 2], one round moves fast to None, and the next check reads None.next and crashes. On even lengths fast always ends on None, so it must be tested first.

Stopping one round early

✗ Wrong
while fast.next and fast.next.next:
✓ Right
while fast and fast.next:

On [1, 2, 3, 4] this stops with slow on node 2, the first middle, but the expected answer is node 3. Odd lengths still pass, which hides the bug.

06

Edge cases

A single node

fast.next is None, so the loop never runs and the head itself is returned.

Two nodes

One round moves slow to the second node and fast to None; the second node is the answer.

07

Complexity

Time
O(n)
Space
O(1)
Fast walks the list once, about n / 2 rounds of two hops. Only two pointers are stored.
08

Middle of the Linked List FAQ

How do I get the first middle on an even-length list?

Start fast one node ahead (fast = head.next), or loop while fast.next and fast.next.next. Palindrome Linked List and Sort List use this version, because they split the list after the first middle.

Is counting the length first any worse?

Both are O(n) time and O(1) space. Counting reads about 1.5n nodes over two passes, against about n for the two pointers. The real gain is that the two-pointer version works in one pass, which matters when the list can only be read once.