Remove Duplicates from Sorted List - Problem

Given the head of a sorted linked list, your task is to remove all duplicate nodes so that each unique value appears only once in the final list.

Since the linked list is already sorted, all duplicate values will be consecutive, making this problem more manageable than if the duplicates were scattered throughout the list.

Goal: Return the head of the modified linked list with duplicates removed, maintaining the sorted order.

Example: If your linked list is 1 → 1 → 2 → 3 → 3, the result should be 1 → 2 → 3.

This is a fundamental linked list manipulation problem that tests your understanding of pointer manipulation and node traversal - essential skills for any software engineer!

Input & Output

example_1.py — Basic duplicate removal
$ Input: [1,1,2]
Output: [1,2]
💡 Note: The linked list 1→1→2 has a duplicate value 1. After removing the duplicate, we get 1→2.
example_2.py — Multiple duplicates
$ Input: [1,1,2,3,3]
Output: [1,2,3]
💡 Note: The linked list has duplicates for both 1 and 3. After removing all duplicates, each unique value appears only once: 1→2→3.
example_3.py — Single node
$ Input: [1]
Output: [1]
💡 Note: A single node has no duplicates, so the list remains unchanged.

Constraints

  • The number of nodes in the list is in the range [0, 300]
  • Each node's value is in the range [-100, 100]
  • The list is guaranteed to be sorted in non-decreasing order

Visualization

Tap to expand
Duplicate Removal VisualizationThink of it like organizing playing cards:Initial sorted deck:AA233Process: Compare adjacent cardsAcurrentAduplicateSkip!2Final clean deck:A23✓ No duplicates!Algorithm StepsStep 1: Start at head of listStep 2: Compare current with nextStep 3: If equal, skip next nodeStep 4: If different, advance currentStep 5: Repeat until end of listWhy This Works• List is sorted → duplicates are adjacent• Only need to check immediate neighbor• Single pass through the listComplexityTime: O(n) | Space: O(1)
Understanding the Visualization
1
Start with sorted cards
Your deck is already sorted: A, A, 2, 3, 3
2
Compare adjacent cards
Look at current card (A) and next card (A) - they're the same!
3
Remove the duplicate
Take out the second A and continue with the first A
4
Continue the process
Now compare A with 2 - different, so move to next position
5
Handle remaining duplicates
When you reach the 3s, remove the duplicate 3 as well
6
Final result
Your clean deck: A, 2, 3 - no duplicates!
Key Takeaway
🎯 Key Insight: Since the linked list is sorted, duplicates are always adjacent. This means we only need to compare each node with its immediate neighbor, avoiding the need for nested loops and achieving optimal O(n) time complexity.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
85.0K Views
High Frequency
~15 min Avg. Time
2.9K 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