Remove Duplicates from Sorted List - Problem
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.
The linked list is sorted in ascending order.
Input & Output
Example 1 — Basic Duplicates
$
Input:
head = [1,1,2]
›
Output:
[1,2]
💡 Note:
The linked list has duplicate 1s. After removing duplicates, we get [1,2] with each element appearing only once.
Example 2 — Multiple Groups
$
Input:
head = [1,1,2,3,3]
›
Output:
[1,2,3]
💡 Note:
We have two groups of duplicates: two 1s and two 3s. Remove one from each group to get [1,2,3].
Example 3 — No Duplicates
$
Input:
head = [1,2,3]
›
Output:
[1,2,3]
💡 Note:
The list has no duplicates, so it remains unchanged.
Constraints
- The number of nodes in the list is in the range [0, 300]
- -100 ≤ Node.val ≤ 100
- The list is guaranteed to be sorted in ascending order
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sorted linked list with duplicates: [1,1,2,3,3]
2
Process
Compare adjacent nodes and skip duplicates
3
Output
List with unique values: [1,2,3]
Key Takeaway
🎯 Key Insight: In sorted lists, duplicates are always adjacent, enabling single-pass removal
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code