Middle of the Linked List (LeetCode 876): Solution and Explanation
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.
Watch it run step by step
Try your own input and write the code →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
- Point both
slowandfastathead. - While
fastis not None andfast.nextis not None, keep looping. - In each iteration, move
slowone node forward andfasttwo nodes forward. - When the loop ends,
fastis either on the last node (odd length) or past the end (even length). - 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]
- Start with
slowandfaston node 1. - First iteration:
slowmoves to 2 andfastmoves to 3. - Second iteration:
fastis on 3 andfast.nextexists, soslowmoves to 3 andfastmoves to 5. - Third iteration:
fast.nextis node 6, so the loop runs again.slowmoves to 4 andfastjumps past 6 to None. - The loop stops because
fastis None, and the function returns node 4, the second middle. For [1, 2, 3, 4, 5] the loop would stop withfaston 5 andslowon 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 slow1class Solution {
2 public ListNode middleNode(ListNode head) {
3 ListNode slow = head, fast = head;
4 while (fast != null && fast.next != null) {
5 slow = slow.next;
6 fast = fast.next.next;
7 }
8 return slow;
9 }
10}1class Solution {
2public:
3 ListNode* middleNode(ListNode* head) {
4 ListNode* slow = head;
5 ListNode* fast = head;
6 while (fast && fast->next) {
7 slow = slow->next;
8 fast = fast->next->next;
9 }
10 return slow;
11 }
12};Time and space complexity
O(n)
The fast pointer covers the list in about n / 2 iterations, and each iteration does constant work.
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.nextas 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.nextwithout checkingfast. On even-length listsfastbecomes None andfast.nextraises an error. - Moving
fasttwice without checking in between, for example in a language where you writefast = fast.next; fast = fast.nextseparately and forget the guard. - Returning
slow.valinstead 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.