Delete the Middle Node of a Linked List (LeetCode 2095): Solution and Explanation
Given the head of a linked list, remove the node at index floor(n / 2) (0-based) and return the head. A single-node list becomes empty.
Intuition
Deleting a node in a singly linked list means changing the next of the node before it. So the real task is to stop one node before the middle.
Counting the length first and walking again works, but two pointers do it in one pass. If fast moves two nodes for every one node slow moves, then when fast runs off the end, slow has gone half the distance.
Started together at the head, slow would stop on the middle itself, one node too far. Giving fast a head start of two nodes (head.next.next) costs slow exactly one round, so it stops just before the middle.
Then slow.next = slow.next.next unlinks the middle. The only case with no node before the middle is a one-node list, which simply becomes empty.
Approach: Slow and fast pointers with a head start
- If
head.nextis null, return null: the only node is the middle. - Set
slow = headandfast = head.next.next. - While
fastandfast.nextare both not null, moveslowone node andfasttwo nodes. - Now
slowis just before the middle. Setslow.next = slow.next.next. - Return
head.
Step-by-step example
head = [1,3,4,7,1,2,6]
- n = 7, so the middle is index 3 (value 7).
slowstarts at index 0,fastat index 2. - Round 1:
fast(index 2) andfast.nextexist.slowmoves to index 1,fastto index 4. - Round 2:
slowmoves to index 2,fastto index 6, the tail. fast.nextis null, so the loop stops withslowon index 2 (value 4), right before the middle.slow.next = slow.next.nextlinks 4 to 1, skipping 7. The result is [1,3,4,1,2,6].
Code: Python, Java, C++
1class Solution:
2 def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
3 if not head.next:
4 return None
5 slow = head
6 fast = head.next.next
7 while fast and fast.next:
8 slow = slow.next
9 fast = fast.next.next
10 slow.next = slow.next.next
11 return head1class Solution {
2 public ListNode deleteMiddle(ListNode head) {
3 if (head.next == null) {
4 return null;
5 }
6 ListNode slow = head;
7 ListNode fast = head.next.next;
8 while (fast != null && fast.next != null) {
9 slow = slow.next;
10 fast = fast.next.next;
11 }
12 slow.next = slow.next.next;
13 return head;
14 }
15}1class Solution {
2public:
3 ListNode* deleteMiddle(ListNode* head) {
4 if (!head->next) {
5 return nullptr;
6 }
7 ListNode* slow = head;
8 ListNode* fast = head->next->next;
9 while (fast && fast->next) {
10 slow = slow->next;
11 fast = fast->next->next;
12 }
13 slow->next = slow->next->next;
14 return head;
15 }
16};Time and space complexity
O(n)
fast reaches the end after about n / 2 rounds, and each round is constant work.
O(1)
Only two pointers are used, and the list is changed in place.
Other approaches
Count, then walk (time O(n), space O(1))
Walk the list once to get n, then walk again n / 2 - 1 steps to the node before the middle and unlink it. Same complexity, two passes, and the n = 1 case still needs its own branch.
Slow, fast and a prev pointer (time O(n), space O(1))
Start both pointers at head, let slow land on the middle, and keep a prev pointer one behind slow to do the unlink. It works but carries one extra pointer that the head start makes unnecessary.
Copy into an array (time O(n), space O(n))
Store all nodes in an array and relink nodes[mid - 1] to nodes[mid + 1]. Simple, but uses O(n) extra memory.
Common mistakes
- Starting
fastatheadas well. Thenslowlands on the middle itself, and you cannot unlink it without the node before it. - Checking only
fast.nextin the loop. Whenfastis already null that dereference fails; checkfastfirst. - Forgetting the single-node list.
head.next.nextdoes not exist there, and the answer is an empty list. - Mixing up the middle for even lengths. For n = 4 the middle is index 2, the second of the two central nodes.
Frequently asked questions
Why does the head start of two nodes work for both odd and even lengths?
fast stops after about (n - 1) / 2 rounds when started at index 2, and slow makes the same number of moves from index 0. Working it out for both parities gives slow at index floor(n / 2) - 1, exactly one before the middle.
Is this the same idea as Middle of the Linked List?
Yes, the same slow and fast pointers find the middle. The difference here is that you need the node before the middle, which is why fast gets a head start.
What does the function return for a list of two nodes?
The middle of a two-node list is index 1. fast starts as null, the loop never runs, and slow (the head) drops its next node, so the result is just the head.