Delete the Middle Node of a Linked List
Medium
Topics
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
Input: head = [1,3,4,7,1,2,6] Output: [1,3,4,1,2,6]
Example 2
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
- If there are fewer than two nodes, return null.
- Walk
slowby one andfastby two, keepingprevone behindslow. 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 listComplexity
- 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 headimport java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode deleteMiddle(ListNode head) {
if (head == null || head.next == null) return null;
ListNode slow = head, fast = head, prev = null;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = slow.next;
return head;
}
}#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {
}
};
class Solution {
public: ListNode* deleteMiddle(ListNode* head) {
if (!head || !head->next) return nullptr;
ListNode* slow = head;
ListNode* fast = head;
ListNode* prev = nullptr;
while (fast && fast->next) {
prev = slow;
slow = slow->next;
fast = fast->next->next;
}
prev->next = slow->next;
return head;
}
};/** Node shape: { val, next }. Build/return plain objects or use this ctor. */ function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
/**
* @param {ListNode} head
* @return {ListNode}
*/ var deleteMiddle = function(head) {
if (!head || !head.next) return null;
let slow = head, fast = head, prev = null;
while (fast && fast.next) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = slow.next;
return head;
};