Delete the Middle Node of a Linked List

Pattern: Fast and slow pointers

medium

To delete the middle you need the node before it. Start fast two nodes ahead, so that when fast runs out, slow stops one node before the middle. Then skip over it.

The problem

Given the head of a linked list of n nodes, delete the middle node, which is node ⌊n / 2⌋ counting from 0, and return the head. For example, 1 → 3 → 4 → 7 → 1 → 2 → 6 loses the 7, and 2 → 1 loses the 1. A single node leaves an empty list.

The function name deleteMiddle matches LeetCode. Use the link above for the exact statement and limits.

Think first

  • Middle of the Linked List lands slow on the middle. Why is that one node too far here?
  • How can you make slow stop one node earlier?
  • What happens with one or two nodes?

Hints

    Brute force, and why it fails

    Count the nodes, then walk to node n / 2 - 1: two passes. The head start gives one pass.

    The key insight

    Shifting where fast starts shifts where slow stops. A head start of one extra round makes slow stop just before the middle, exactly where a deletion needs to stand.

    Dry run

    10slow
    31 
    42fast
    73 
    14 
    25 
    66 

    The list is 1 → 3 → 4 → 7 → 1 → 2 → 6, 7 nodes. The middle is index 3, the 7. slow starts on index 0 and fast on index 2.

    Step 1 of 4

    The plan

    if head.next is null: return null
    slow = head, fast = head.next.next
    while fast is not null and fast.next is not null:
        slow = slow.next; fast = fast.next.next
    slow.next = slow.next.next
    return head
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    One pass.

    Edge cases

    • One node: empty.
    • Two nodes: the second is removed.
    • Even lengths: the second middle (index n / 2) is removed.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode deleteMiddle(ListNode head) {
            if (head.next == null) return null;     // one node: it is the middle
            ListNode slow = head, fast = head.next.next;   // fast starts one round ahead, so slow stops before the middle
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            slow.next = slow.next.next;
            return head;
        }
    }

    C++

    class Solution {
    public:
        ListNode* deleteMiddle(ListNode* head) {
            if (head->next == NULL) return NULL;    // one node: it is the middle
            ListNode *slow = head, *fast = head->next->next;   // fast one round ahead: slow stops before the middle
            while (fast != NULL && fast->next != NULL) {
                slow = slow->next;
                fast = fast->next->next;
            }
            slow->next = slow->next->next;
            return head;
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def deleteMiddle(self, head: ListNode) -> ListNode:
            if head.next is None:
                return None                         # one node: it is the middle
            slow, fast = head, head.next.next       # fast one round ahead, so slow stops before the middle
            while fast is not None and fast.next is not None:
                slow, fast = slow.next, fast.next.next
            slow.next = slow.next.next
            return head

    Common mistakes

    • Using the plain middle loop, which stands on the middle and cannot delete it.
    • Forgetting the one-node case, where head.next.next would crash.
    • Deleting the first middle for even lengths.

    Similar problems