Remove Duplicates from Sorted List — Problem & Solution
Remove Duplicates from Sorted List solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Remove Duplicates from Sorted List is an easy coding problem on two pointers, asked in Amazon, Microsoft and Adobe interviews.
- Difficulty: Easy
- Topics: Two Pointers
- Asked at: Amazon, Microsoft, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given the head of a singly linked list sorted in non-decreasing order. Delete nodes so that every value appears exactly once, and return the head of the list, which stays sorted.
Example 1
Input: head = [1,1,2]
Output: [1,2]
Example 2
Input: head = [1,1,2,3,3]
Output: [1,2,3]
Explanation: One node of each value is kept; the list is still sorted.
Example 3
Input: head = []
Output: []
Constraints
The number of nodes in the list is in the range [0, 300]-100 <= Node.val <= 100The list is sorted in non-decreasing order
Hints for Remove Duplicates from Sorted List
Hint 1
In a sorted list, equal values sit next to each other.
Hint 2
Walk the list once, comparing each node with the node after it.
Hint 3
When the next node has the same value, unlink it (
cur.next = cur.next.next) and stay put; otherwise move on.
How to solve Remove Duplicates from Sorted List
Sorting puts all copies of a value in one consecutive run, so a duplicate is always the immediate successor of a node with the same value. Unlinking such successors in a single pass leaves one node per run.
Approach
- Set
cur = head. - While
curandcur.nextexist: ifcur.next.val == cur.val, skip it withcur.next = cur.next.next; otherwise advancecur = cur.next. - Return
head.
Why it works
cur only advances to a node with a new value, and before it does, every following node equal to cur has been unlinked. So each run collapses to its first node and nothing else is removed.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Do not advance
curafter removing a duplicate — the newcur.nextmay be another copy. - Handle the empty list: the loop must not touch
head.nextwhenheadis null.
Reference solution
Python
from typing import Optional
def deleteDuplicates(head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
while cur and cur.next:
if cur.next.val == cur.val:
cur.next = cur.next.next
else:
cur = cur.next
return headJava
public static ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
while (cur != null && cur.next != null) {
if (cur.next.val == cur.val) cur.next = cur.next.next;
else cur = cur.next;
}
return head;
}C++
ListNode* deleteDuplicates(ListNode* head) {
ListNode* cur = head;
while (cur && cur->next) {
if (cur->next->val == cur->val) cur->next = cur->next->next;
else cur = cur->next;
}
return head;
}JavaScript
var deleteDuplicates = function(head) {
var cur = head;
while (cur !== null && cur.next !== null) {
if (cur.next.val === cur.val) cur.next = cur.next.next;
else cur = cur.next;
}
return head;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 165 two pointers problems · the whole catalogue
Learn the technique: Two Pointers Technique