Middle of the Linked List
Start two pointers at the head. Each round, slow moves 1 step and fast moves 2 steps. Fast always has walked exactly twice as far as slow. So when fast runs out of list, slow is exactly halfway — the middle — in a single pass.
Fast is always twice as far from the head as slow, so the moment fast reaches the end, slow is at position n / 2 — the middle.
Problem
You are given the head of a singly linked list. Return the middle node of the list.
If the list has an even number of nodes, there are two middle nodes. Return the second one.
Return the node itself (the list from that node onward), not just its value.
head, the first node of a singly linked list.
The middle node. For an even length, the second of the two middle nodes.
5 nodes, so the middle is the 3rd node (value 3). The answer is printed as the list starting from it.
6 nodes, so the two middles are 3 and 4. We return the second one, 4.
- • The list has between 1 and 100 nodes.
- • 1 ≤ Node.val ≤ 100
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
In an array I would just return
a[n / 2]. What stops me from doing that in a linked list? - 2.
Do I know the length of the list before I start walking?
- 3.
If I counted the length first, how many steps from the head would the middle be?
- 4.
Can two pointers moving at different speeds tell me when I am halfway?
- 5.
For 6 nodes, should I stop on the 3rd node or the 4th? What about 5 nodes?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Store every node pointer in a vector, return v[n/2] | O(N) | O(N) |
| Better | Pass 1 counts n, pass 2 walks n/2 steps; no extra memory | O(N) | O(1) |
| Optimal | slow moves 1, fast moves 2; when fast hits the end, slow is the middle | O(N) | O(1) |
Why does this belong to this pattern?
- “middle node”We need the point halfway along a list whose length we don't know → one pointer at double speed.
- “singly linked list”No indexes and no going back, so we can't jump to n / 2. We must find it while walking forward.
- “two middles → return the second”This decides the loop condition: `fast && fast->next` stops on the second middle.
This is the anchor of the Middle / split sub-pattern. Fast moves 2, slow moves 1, so when fast reaches the end, slow is halfway. Unlike cycle detection, there is no loop here: fast does reach null, and the only question is where slow is when it does. The next questions use this exact loop as a first step: to cut the list in half (palindrome, twin sum) or to stop one node early (delete the middle).
Don't make this mistake
The "wrong" loop stops one round early on even lengths. On [1, 2, 3, 4, 5, 6] it returns 3, but this problem wants the second middle, 4. Both loops are useful — the first-middle version is what you want when splitting a list into two halves (see the next questions) — but read which middle the problem asks for.
On an even length, fast jumps to null. The wrong order then reads fast->next on a null pointer and crashes. && checks left to right, so put the null check first.
The answer is the node that is already in the list, with the rest of the list after it. A new node with the same value has no next, so the output would be [3] instead of [3, 4, 5].
Pattern confusion: in cycle detection we wait for fast to meet slow. Here there is no cycle, so they never meet and fast runs off the end. For the middle, the stop signal is fast reaching the end, not fast catching slow. Also, starting fast one node ahead changes which middle you land on.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Fast & Slow translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.