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
Remove Duplicates from Sorted ListInput: [1,1,2,3,3] → Output: [1,2,3]11233Original List (duplicates marked with dashed border)123Result: Each element appears only once
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
Asked in
Facebook 32 Amazon 28 Microsoft 24 Google 19
125.0K Views
High Frequency
~15 min Avg. Time
3.6K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen