AlgoMaster Logo

Delete the Middle Node of a Linked List

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We have a singly linked list and need to delete the node at position floor(n/2) (0-indexed). So for a list of 7 nodes, the middle is at index 3. For a list of 4 nodes, the middle is at index 2. For a single node, the middle is at index 0, which means we delete the only node and return null.

A singly linked list has no random access, so reaching any position means walking from the head. Deletion adds a second requirement: removing a node rewires the next pointer of the node before it, so the traversal has to stop one node short of the middle.

That reduces the problem to finding position floor(n/2) - 1 efficiently. One option is to count the nodes first and walk there in a second pass. The other is to find it in a single traversal with slow and fast pointers.

Key Constraints:

  • Number of nodes in range [1, 10^5] → A full traversal is unavoidable since there is no random access, and O(n) time handles 100,000 nodes comfortably. The question is how many passes the traversal takes, not whether we can beat O(n).
  • 1 <= Node.val <= 10^5 → Values play no role here. The middle is defined purely by position.
  • n >= 1 → The list is never empty, but a single-node list is possible: its middle is the head itself, and deleting it returns null.

Approach 1: Two-Pass (Count then Delete)

Intuition

If we knew the total number of nodes n, we could compute the middle index and walk to the node before it. Two passes provide both pieces: the first counts the nodes, the second stops at position floor(n/2) - 1 and rewires its next pointer to skip the middle node.

One edge case needs separate handling: a list with a single node has its middle at the head, so we delete it by returning null before either pass runs.

Algorithm

  1. Traverse the entire list to count the total number of nodes n.
  2. If n == 1, return null (the only node is the middle).
  3. Calculate the middle index: mid = n / 2.
  4. Traverse the list again, stopping at node mid - 1.
  5. Set node.next = node.next.next to skip the middle node.
  6. Return head.

Visualization and Code

Loading animation...

The counting pass exists only to locate the middle. The next approach finds it during the traversal itself, cutting the work to a single pass.

Approach 2: Slow and Fast Pointers

Intuition

Move a fast pointer two steps for every one step of a slow pointer. When fast reaches the end of the list, slow is at the middle.

Stopping at the middle is not enough, though: deletion needs the node before it. Starting fast two nodes ahead, at head.next.next instead of head, makes the loop exit one slow-step earlier. When fast runs out of list, slow is on the node before the middle instead of on the middle itself.

Algorithm

  1. If head.next is null (single node), return null.
  2. Initialize slow = head and fast = head.next.next.
  3. While fast is not null and fast.next is not null, advance slow by one step and fast by two steps.
  4. When the loop ends, slow.next is the middle node. Set slow.next = slow.next.next to delete it.
  5. Return head.

Visualization and Code

Loading animation...