Remove Duplicates From a Sorted List
L6 Medium Linked List
Concept
Sorted lists keep duplicates adjacent, so a single pass that skips equal neighbors is enough to leave only distinct values.
Given the head of a sorted linked list, delete all nodes that appear more than once, keeping only one copy of each value. Return the head.
Examples
▸ head = [1, 1, 2]
→ [1, 2]
▸ head = [1, 1, 2, 3, 3]
→ [1, 2, 3]
▸ head = [1, 1, 1]
→ [1]
Progressive Hints
Hint 1 · Nudge
In a sorted list, duplicates always sit right next to each other, so only local comparisons matter.
Hint 2 · Plan
Walk the list with the current node. While the next node carries the same value, skip it. Then step to the next distinct value.
Hint 3 · Approach
cur = head. While cur and cur.next exist: if cur.val == cur.next.val then cur.next = cur.next.next else cur = cur.next. Return head.
All hints are out. Take a breath and give it a shot.
Output
// Run your code to see the output here.