15% off all plans & roadmaps Use code
See plansLeetCode 82 · Solution & editorial
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Remove Duplicates from Sorted List II is a Linked List Manipulation Patterns problem, in the In-place Reversal subpattern. The pattern covers in-place reversal, merging sorted lists, reordering, and intersection detection
Master in-place linked list reversal including full reversal, partial reversal, and k-group reversal techniques.
The reference solution runs in O(n) time and O(1) space.
Remove Duplicates from Sorted List II has been reported in interviews at Amazon, Bloomberg.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Remove Duplicates from Sorted List II is a Linked List Manipulation Patterns problem, in the In-place Reversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.