Delete the Middle Node of a Linked List (LeetCode 2095): Solution and Explanation

Medium
Linked List
Two Pointers

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

  1. If head.next is null, return null: the only node is the middle.
  2. Set slow = head and fast = head.next.next.
  3. While fast and fast.next are both not null, move slow one node and fast two nodes.
  4. Now slow is just before the middle. Set slow.next = slow.next.next.
  5. Return head.

Step-by-step example

head = [1,3,4,7,1,2,6]
  1. n = 7, so the middle is index 3 (value 7). slow starts at index 0, fast at index 2.
  2. Round 1: fast (index 2) and fast.next exist. slow moves to index 1, fast to index 4.
  3. Round 2: slow moves to index 2, fast to index 6, the tail.
  4. fast.next is null, so the loop stops with slow on index 2 (value 4), right before the middle.
  5. slow.next = slow.next.next links 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 head
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

fast reaches the end after about n / 2 rounds, and each round is constant work.

Space

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 fast at head as well. Then slow lands on the middle itself, and you cannot unlink it without the node before it.
  • Checking only fast.next in the loop. When fast is already null that dereference fails; check fast first.
  • Forgetting the single-node list. head.next.next does 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.