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.
- The number of nodes in the list is in the range [1, 100].
- 1 <= Node.val <= 100
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.
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.
Approach
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.
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.
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.
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.
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.
Code in Python, C++ and Java
Common pitfalls
Checking only fast.next
while fast.next:
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
while fast.next and fast.next.next:
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.
Edge cases
fast.next is None, so the loop never runs and the head itself is returned.
One round moves slow to the second node and fast to None; the second node is the answer.
Complexity
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.