#2095
Medium Algorithms Delete the middle node of a linked list
Linked List Two Pointers
59.5% acceptance
Feb 25, 2026
4904
105
You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.
The middle node of a linked list of size n is the floor(n/2)th node from the start using 0-based indexing.
For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn delete_middle(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
// Collect values, remove middle, rebuild
let mut vals = Vec::new();
let mut cur = &head;
while let Some(node) = cur {
vals.push(node.val);
cur = &node.next;
}
let n = vals.len();
if n == 0 {
return None;
}
let mid = n / 2;
vals.remove(mid);
let mut dummy = Box::new(ListNode::new(0));
let mut cur = &mut dummy;
for &v in &vals {
cur.next = Some(Box::new(ListNode::new(v)));
cur = cur.next.as_mut().unwrap();
}
dummy.next
}
}