Delete the Middle Node of a Linked List
Delete the Middle Node of a Linked List is LeetCode 2095 (Medium). You get the head of a singly linked list with n nodes. Delete the node at index ⌊n / 2⌋, counting from 0, and return the head.
- When
nis even there are two centre nodes, and this index picks the second one. ncan be 1. Deleting the only node leaves an empty list, so the answer is null.
- The number of nodes in the list is in the range [1, 10⁵].
- 1 <= Node.val <= 10⁵
Intuition
To remove a node from a singly linked list, you change the next link of the node before it. So the node you really need is at index ⌊n / 2⌋ − 1, just before the middle.
A fast pointer that moves two nodes for every one the slow pointer moves finds a middle in a single pass. Give the fast pointer a head start of two nodes. When it runs out of list, the slow pointer has fallen one node short of the middle, which is exactly where the relink happens.
Start fast two nodes ahead, walk until it reaches the end, then skip the node after slow.
You need a position defined by the length of the list, but you cannot know the length without walking the list. A second pointer at double speed measures the length for you during the same walk.
Approach
Before reading on, draw 1 → 2 → 3 → 4 and mark which node must change its next link to delete index 2. Then work out how far ahead a double-speed pointer must start so the slow one stops there. Aim for one pass and O(1) space.
Handle a one-node list first
If head.next is null, the middle is the head itself and no node comes before it to relink, so return null. Every other list has at least two nodes, which the next step relies on.
Start slow on the head and fast two nodes later
Set slow = head and fast = head.next.next. After k moves slow sits at index k and fast at index 2 + 2k. That head start of two is what puts slow one node before the middle instead of on it.
Move both until fast runs out
While fast and fast.next both exist, move slow one node and fast two. The loop stops at the first k where 2 + 2k reaches n − 1 or beyond, and for every n ≥ 2 that k is exactly ⌊n / 2⌋ − 1.
Unlink the middle
Set slow.next = slow.next.next. The middle node can no longer be reached from the head. The head itself never changes, since index 0 is never the middle once n ≥ 2. In C++, delete the removed node so it does not leak.
Code in Python, C++ and Java
Common pitfalls
Starting both pointers on the head
slow, fast = head, head
slow, fast = head, head.next.next
Then slow stops on the middle itself, and a singly linked list cannot step back to the node before it. On [1, 2, 3, 4] it stops on 3, so slow.next = slow.next.next deletes 4 and returns [1, 2, 3] instead of [1, 2, 4].
Guarding the loop with fast.next.next
while fast.next and fast.next.next:
while fast and fast.next:
With the two-node head start, fast can already be null before the loop. On [2, 1], head.next.next is null, so reading fast.next raises an AttributeError before anything is deleted.
Edge cases
[2, 1]fast starts as null, so the loop never runs. slow is still the head, the second node is skipped, and the answer is [2].
Complexity
fast reaches the end after about n / 2 moves, so the walk visits each node at most once. Only two pointers are kept, whatever the length of the list.Delete the Middle Node of a Linked List FAQ
How is this different from Delete Node in a Linked List?
In Delete Node in a Linked List you get the node itself and no head, so you copy the next value into it; here you get the head, so you can reach the node before the middle and relink it.
Is counting the nodes first any slower?
Not really. Counting takes n hops and walking to index ⌊n / 2⌋ − 1 takes about n / 2 more. The fast and slow pointers also make about n and n / 2 hops. Both are O(n) time and O(1) space; the pointer pair just does it in one loop.
Why do some solutions add a dummy node?
A dummy node in front of the head lets slow start one node earlier and fast start on the head. Then a one-node list needs no special branch: slow stays on the dummy, its next becomes null, and you return dummy.next.