Middle of the Linked List
Return the middle node of a singly linked list in a single pass using fast and slow pointers.
01The problem
Given the head of a singly linked list, return the middle node of the linked list.
If there are two middle nodes (i.e. the list length is even), return the second middle node.
Do this in a single pass with O(1) extra memory.
| Input | Output | Why |
|---|---|---|
[1, 2, 3, 4, 5] | [3, 4, 5] | 3 is the exact middle node (index 2 of 5) |
[1, 2, 3, 4, 5, 6] | [4, 5, 6] | Length is 6; middle nodes are 3 and 4, return second (4) |
[1] | [1] | Single node is its own middle |
Constraints: 1 to 100 nodes, node values between 1 and 100.
02How to spot it
- You need a node at a position proportional to the total length (e.g.
n / 2). - The total length
nis not known in advance. - The problem asks for a single pass without allocating extra collections or arrays.
- Advance two pointers at different relative speeds: one moves twice as fast as the other. When the faster pointer reaches the tail, the slower pointer is at the midpoint.
So: fast and slow pointers (tortoise and hare).
03Pseudocode
slow = head ; fast = head
while fast != null and fast.next != null:
slow = slow.next
fast = fast.next.next
return slow- Initialize
slowandfastpointers athead. - While
fasthas at least two steps available ahead, moveslowby 1 andfastby 2. - When
fastreaches the end,slowis resting at the middle node.
04Watch it work
linked list
Initialize slow and fast pointers at head (index 0, value 1).
Pseudocode
- slow = head ; fast = head
- while fast != null and fast.next != null:
- slow = slow.next
- fast = fast.next.next
- return slow
Variables
- slow
- 1
- fast
- 1
05Step-by-step dry run
- 1
linked list
1021324354slowfastInitialize slow and fast pointers at head (index 0, value 1).
slow = 1fast = 1
- 2
linked list
1021324354slowfastBoth fast (index 0) and fast.next (index 1) exist. Advance runners.
slow = 1fast = 1
- 3
linked list
1021324354slowfastAdvance slow by 1 step to index 1 (2); advance fast by 2 steps to index 2 (3).
slow = 2fast = 3
- 4
linked list
1021324354slowfastBoth fast (index 2) and fast.next (index 3) exist. Advance runners.
slow = 2fast = 3
- 5
linked list
1021324354slowfastAdvance slow by 1 step to index 2 (3); advance fast by 2 steps to index 4 (5).
slow = 3fast = 5
- 6
linked list (middle reached)
1021324354slowFast runner cannot take 2 more steps. Slow runner rests at middle node (index 2, value 3).
middle = 3sublist = 3,4,5
06Java solution
public class MiddleOfLinkedList {
static class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public static ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
// Traverse while fast can safely advance two steps
while (fast != null && fast.next != null) {
slow = slow.next; // 1 step
fast = fast.next.next; // 2 steps
}
return slow; // fast at end -> slow at middle
}
public static void main(String[] args) {
// Test 1: Odd length [1, 2, 3, 4, 5]
ListNode head1 = new ListNode(1);
head1.next = new ListNode(2);
head1.next.next = new ListNode(3);
head1.next.next.next = new ListNode(4);
head1.next.next.next.next = new ListNode(5);
ListNode mid1 = middleNode(head1);
printList(mid1); // 3 4 5
// Test 2: Even length [1, 2, 3, 4, 5, 6]
ListNode head2 = new ListNode(1);
head2.next = new ListNode(2);
head2.next.next = new ListNode(3);
head2.next.next.next = new ListNode(4);
head2.next.next.next.next = new ListNode(5);
head2.next.next.next.next.next = new ListNode(6);
ListNode mid2 = middleNode(head2);
printList(mid2); // 4 5 6
}
private static void printList(ListNode node) {
ListNode curr = node;
while (curr != null) {
System.out.print(curr.val + (curr.next != null ? " " : ""));
curr = curr.next;
}
System.out.println();
}
}3 4 5 4 5 6
Tricky lines, decoded
while (fast != null && fast.next != null): Both checks are mandatory. For odd lengths,fast.next == nullstops the loop at the tail node. For even lengths,fast == nullstops the loop afterfaststeps beyond the tail.return slow: Starting both atheadnaturally causes an even-length list to return the second middle node (indexn / 2).
07Why it works
At any point after k loop iterations, slow has traversed k steps and fast has traversed 2k steps.
- For an odd-length list with
2m + 1nodes,fastlands on the last node (index2m) aftermiterations.slowis at indexm, the exact middle. - For an even-length list with
2mnodes,faststeps out tonullaftermiterations.slowis at indexm, which is the second of the two middle elements.
Invariant: After k steps, slow is at node index k and fast is at node index 2k.
08Time and space
- Time: O(n).
fastvisits each node at most once, takingn / 2iterations. - Space: O(1). Only two pointer references are maintained.
09Edge cases
| Input | Output | Why it matters |
|---|---|---|
[1] | [1] | Single node is its own middle |
[1, 2] | [2] | Two nodes; returns the second middle |
[1, 2, 3] | [2, 3] | Minimal odd-length list |
[1, 2, 3, 4, 5, 6] | [4, 5, 6] | Even length returns second middle |
[1, 2, 3, 4, 5] | [3, 4, 5] | Odd length returns exact middle |
10Common mistakes
- Only checking
fast.next != null. If list length is even,fastadvances tonull, and evaluatingfast.nextin the next iteration throws aNullPointerException. - Starting
fastathead.next. This shiftsslowone step earlier, returning the first middle instead of the required second middle for even lengths. - Copying nodes into an array. Allocates
O(n)auxiliary memory when two pointers solve it inO(1).
11Other approaches
| Approach | Time | Space | Use when |
|---|---|---|---|
| Two-pass count | O(n) | O(1) | First pass counts n, second walks n / 2 |
| Fast & slow | O(n) | O(1) | Default single-pass interview solution |
| Copy to array | O(n) | O(n) | Only if repeated index lookups needed |
12In the interview
- Where is this pattern applied?
- Palindrome Linked List (Problem 49): Splitting the list in half before reversing the second half.
- Linked List Sort (Merge Sort on Lists): Splitting a list into two equal halves.
- Reorder List (LeetCode 143): Finding the split point to interweave nodes.
13Check your understanding
Quiz · 2 questions
0 of 2 answered
1. Why is it necessary to check both fast != null and fast.next != null in the loop condition?
2. How would you adjust the algorithm if an interviewer specifically asks for the first middle node on an even-length list?
14Solve it yourself
Apply this two-pointer strategy to LeetCode 143 (Reorder List):
- Find the middle node using slow and fast pointers.
- Reverse the second half of the list in place.
- Merge the first half and reversed second half by alternating nodes.