Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II is LeetCode 82 (Medium). Given the head of a sorted linked list, delete all nodes whose value occurs more than once, so only the values that appeared exactly once are left. Return the new head, still sorted.
- A repeated value keeps no copy at all. Keeping one copy is the easier first version.
- The head itself may be deleted, and the answer may be an empty list.
- 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.
Intuition
In a sorted list each value forms one block of neighbours. A block survives only if it has length one. You cannot tell that from the first node alone, so you look ahead: if the next node holds the same value, the whole block must go.
- Look ahead, then decide: walk to the last node of the block, then link the node before the block straight to whatever comes after it.
- Dummy node: the node before the first block does not exist yet, so a placeholder sits in front of the head. Deleting the first block is then the same operation as deleting any other block.
When a value's fate depends on how many times it appears, and the list is sorted, scan block by block and keep a pointer to the last node you are sure about.
Approach
Before reading on, decide which node's next you change when a block of equal values is thrown away, and what that node is when the very first block goes. Try it on [1, 1, 2]. Aim for O(n) time and O(1) extra space.
Put a placeholder before the head
Create dummy with dummy.next = head and set prev = dummy. prev always marks the last node that is certain to stay, and the placeholder plays that part until a real node earns it.
Check whether the current value repeats
With head on the first node of a block, compare it with head.next. Only a match tells you this block is longer than one node, and the list being sorted means no copy can show up later.
Skip the whole block
If it repeats, move head along while the next value is still equal, so head ends on the block's last node. Then set prev.next = head.next. prev stays where it is, because the node after the block has not been checked yet.
Keep a single node
If it does not repeat, the node is safe, so move prev onto it. Either way, step head forward to the first node of the next block. At the end return dummy.next, which may be null.
Code in Python, C++ and Java
Common pitfalls
Moving prev after cutting a block
prev.next = head.next prev = prev.next
prev.next = head.next
On [1, 2, 2, 3, 3], after the 2s are cut prev would jump onto the first 3 before anyone knows 3 repeats. The next cut then starts from that 3 itself, so one 3 survives and the output is [1, 3] instead of [1].
Comparing with the previous value
if head.val == last_val: # skip head
if head.next and head.val == head.next.val:
Looking back only catches the second and later copies. On [1, 1, 2] the first 1 is kept because nothing equal came before it, giving [1, 2] instead of [2].
Edge cases
prev is still the placeholder, so the cut makes dummy.next point at 2 and the returned head changes without a special case.
Each block is cut from the placeholder, dummy.next ends as null, and the answer is an empty list.
Complexity
head visits each node once, whether the outer loop or the inner block loop moves it. The placeholder and two pointers are the only extra memory.Remove Duplicates from Sorted List II FAQ
Can I solve it with a hash map of counts?
Yes: count every value in one pass, then rebuild the chain keeping only values with count 1. It works even on an unsorted list, but it spends O(n) memory that the sorted order makes unnecessary.
Is there a recursive version?
Yes. If the head starts a block, skip the block and recurse on what follows it; otherwise set head.next to the recursive result and return head. It reads cleanly but uses O(n) call stack for a list with no repeats.