Remove Duplicates from Sorted List
Remove Duplicates from Sorted List is LeetCode 83 (Easy). You get the head of a linked list sorted in ascending order. Delete nodes so that every value appears exactly once, and return the head of the list that is left.
- One copy of each value stays. If every copy must go instead, that is the version II problem.
- The list can be empty, and it stays sorted after the deletions.
- 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
Because the list is sorted, equal values always sit next to each other. A node therefore only ever needs to look at the node right behind it: if that neighbour has the same value, it is a spare copy and can be unlinked by pointing past it.
The pointer moves forward only when the neighbour holds a new value. After an unlink, the node behind it is new and has not been checked yet, so staying put lets a run of three or four equal values collapse one node at a time until a different value arrives.
"Sorted" plus "duplicates" in a linked list means copies are neighbours, so one walk that compares each node with its next is enough. No set or extra list is needed.
Approach
Before reading on, write the loop condition and decide what happens to the pointer right after you unlink a node. Test your idea on a list of three equal values. Aim for O(n) time and O(1) extra space.
Start at the head
Put cur on the first node. The first node can never be a spare copy, because it is the first time its value is seen, so the head of the answer is the original head and no placeholder node is needed.
Compare with the next node
While both cur and cur.next exist, check whether they hold the same value. The loop needs both, since the comparison reads cur.next.val and an empty or finished list has nothing to compare.
Unlink a copy and stay
If the values match, set cur.next = cur.next.next. That drops the copy from the chain. Do not move cur, because its new neighbour could be one more copy of the same value.
Move on a new value
If the values differ, step cur to the next node. Every node behind cur now holds a distinct value, so the work before it is finished for good.
Code in Python, C++ and Java
Common pitfalls
Moving forward after every unlink
if cur.val == cur.next.val:
cur.next = cur.next.next
cur = cur.nextif cur.val == cur.next.val:
cur.next = cur.next.next
else:
cur = cur.nextOn [1, 1, 1] the first unlink leaves 1 → 1, then cur jumps onto the second 1 and the loop ends, returning [1, 1]. Only a step on a new value is safe.
Checking only cur.next in the loop
while cur.next:
while cur and cur.next:
For an empty list head is null, so cur.next throws before anything happens. The extra check costs nothing and covers the case the constraints allow (0 nodes).
Edge cases
cur never moves. Each pass unlinks one more copy until cur.next is null, leaving the single head node.
Every comparison finds a new value, so cur walks to the last node and nothing is unlinked.
Complexity
cur forward one node, and each of those can happen at most n times. Only one pointer is stored.Remove Duplicates from Sorted List FAQ
What if the list is not sorted?
Copies are no longer neighbours, so comparing with the next node misses them. Keep a set of values seen so far and unlink any node whose value is already in it; that costs O(n) extra space.
Should C++ code free the unlinked nodes?
In real code, yes: save cur->next in a temporary, relink, then delete the temporary. The judge does not check for leaks, which is why many posted answers skip it.
Why is the array version of this task harder to write?
An array has to copy kept values forward into a write position because it cannot drop a slot. A list just changes one next pointer per removed node and nothing else moves.