LeetCode #2095 Medium

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 n is even there are two centre nodes, and this index picks the second one.
  • n can be 1. Deleting the only node leaves an empty list, so the answer is null.
Constraints
  • The number of nodes in the list is in the range [1, 10⁵].
  • 1 <= Node.val <= 10⁵
linked-listtwo-pointers
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def deleteMiddle(self, head):
▶3 if head.next is None:
▶4 return None
▶5 slow, fast = head, head.next.next
▶6 while fast and fast.next:
▶7 slow = slow.next
▶8 fast = fast.next.next
▶9 slow.next = slow.next.next
▶10 return head
linked list10head314273142566nullhead has a next node → at least two nodes
nodes7
middle index3
The list has 7 nodes, so the node to delete is at index ⌊7 / 2⌋ = 3. To unlink it, the walk has to stop on the node before it, index 2.
linked list10head314273142566nullslowfastslow on 0, fast two nodes ahead on 2
slowindex 0
fastindex 2
fast starts two nodes after the head. After k moves slow is at index k and fast at index 2 + 2k, and that head start of two is what will leave slow one node before the middle instead of on it.
linked list10head314273142566nullslowfastfast had a next node → slow to 1, fast to 4
slowindex 1
fastindex 4
Both fast and the node after it exist, so there is room for two more steps: slow moves one node to index 1 and fast moves two, to index 4. Fast is always 2 + 2 × 1 = 4.
linked list10head314273142566nullslowfastfast had a next node → slow to 2, fast to 6
slowindex 2
fastindex 6
Both fast and the node after it exist, so there is room for two more steps: slow moves one node to index 2 and fast moves two, to index 6. Fast is always 2 + 2 × 2 = 6.
linked list10head314273142566nullslowfastfast has no next node → stop with slow on 2
slowindex 2
fastindex 6
Fast is on the last node, so it cannot take two more steps. The walk stops, and slow is on index 2, exactly one before the middle at index 3.
linked list10head314273142566nullslowslow skips over node 3 → links to node 4
slowindex 2
removed7
Setting slow's next link to the node after the middle bypasses the middle node with value 7. Nothing points to it any more, so it is gone from the list; in C++ it is also freed here.
linked list10head3142132465nullanswer: 1 → 3 → 4 → 1 → 2 → 6
answer[1, 3, 4, 1, 2, 6]
The head never changed, because index 0 is never the middle once the list has two or more nodes. The function returns it, and the list now reads 1, 3, 4, 1, 2, 6.
05

Common pitfalls

Starting both pointers on the head

✗ Wrong
slow, fast = head, head
✓ Right
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

✗ Wrong
while fast.next and fast.next.next:
✓ Right
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.

06

Edge cases

Two nodes, e.g. [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].

07

Complexity

Time
O(n)
Space
O(1)
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.
08

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.