#83
Easy Algorithms Remove duplicates from sorted list
Linked List
56.3% acceptance
Jan 12, 2026
9794
364
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut head = head;
let mut current = &mut head;
while let Some(node) = current {
while node.next.is_some() && node.next.as_ref().unwrap().val == node.val {
node.next = node.next.as_mut().unwrap().next.take();
}
current = &mut node.next;
}
head
}
}