Delete the Middle Node of a Linked List

Medium

Topics
Linked ListTwo Pointers

Given the head of a linked list, delete the middle node (the node at 0-indexed position floor(n/2)) and return the head. If the list has one node, return an empty list.

Example 1

1347126null
deleted
Index 3 goes. Deleting needs the node before it, so the slow pointer is started one step back (or trails a previous pointer) — otherwise you find the middle and cannot unlink it.
Linked list values from head to tail.
Input:  head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]

Example 2

1234null
deleted
Input:  head = [1,2,3,4]
Output: [1,2,4]

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5
Editorial — intuition, approach, complexity and pitfalls

Intuition

Deleting a node from a singly linked list means rewiring its predecessor, so finding the middle is not enough — we need the node before it.

Slow/fast pointers find the middle in one pass. Carrying a third pointer one step behind slow gives the predecessor at the same time, so the deletion is a single assignment once the walk ends.

The loop condition matters here in a way it did not for simply returning the middle. With while fast and fast.next, slow lands on index n/2 — which is the node this problem defines as the middle for both parities. A list of one node is special: there is no middle to keep, so the answer is an empty list, handled by an early return.

Approach

  1. If there are fewer than two nodes, return null.
  2. Walk slow by one and fast by two, keeping prev one behind slow.
  3. prev.next = slow.next; return the head.

Dry Run

1 -> 3 -> 4 -> 7 -> 1 -> 2 -> 6      n = 7, middle index 3 (value 7)

prev=null slow=1 fast=1
step      prev=1    slow=3  fast=4
step      prev=3    slow=4  fast=1
step      prev=4    slow=7  fast=6
fast.next is null -> stop

prev is 4, slow is 7  ->  4.next = 7.next
answer 1 -> 3 -> 4 -> 1 -> 2 -> 6

single node [1]: early return -> empty list

Complexity

  • Time — O(n): the fast pointer traverses once.
  • Space — O(1): three pointers. Counting the length first and then walking half of it is also O(1) but needs two passes.

Common Pitfalls

  • Finding the middle without tracking the predecessor, then having to re-walk the list to find it.
  • Missing the single-node case, which must return null rather than the unchanged node.
  • Using the other loop condition, which lands on the first of two middles — the wrong node for this problem's definition.
  • Copying the next node's value over the current one instead of rewiring. That works for a node with a successor but fails on the last node, and it mutates data the caller may hold.

Full Solution — all languages

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def deleteMiddle(self, head):
        if not head or not head.next:
            return None
        slow = fast = head
        prev = None
        while fast and fast.next:
            prev = slow
            slow = slow.next
            fast = fast.next.next
        prev.next = slow.next
        return head
SensAI
Beta
Tap the mic to stop voice mode