LeetCode #83 Easy

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.
Constraints
  • 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.
linked-list
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
▶3 cur = head
▶4 while cur and cur.next:
▶5 if cur.val == cur.next.val:
▶6 cur.next = cur.next.next
▶7 else:
▶8 cur = cur.next
▶9 return head
11233curcur starts on the head, 1
cur1
The first node is the first time its value appears, so it always stays. That is why the answer can start from the original head.
11233curnextnext is 1 too → a spare copy
cur1
next1
The list is sorted, so a copy of 1 can only sit right behind it. This one is a copy and has to leave the chain.
11233cur1 now points past the copy; cur stays
cur1
next2
One pointer change drops the copy. cur does not move, because the node now behind it has not been checked and could be another 1.
11233curnextnext is 2, not 1 → a new value
cur1
next2
2 is bigger than 1, and the list is sorted, so no more copies of 1 can follow.
11233curcur steps onto 2
cur2
Everything before 2 now holds one copy of each value, so that part of the list is finished and never looked at again.
11233curnextnext is 3, not 2 → a new value
cur2
next3
3 is bigger than 2, and the list is sorted, so no more copies of 2 can follow.
11233curcur steps onto 3
cur3
Everything before 3 now holds one copy of each value, so that part of the list is finished and never looked at again.
11233curnextnext is 3 too → a spare copy
cur3
next3
The list is sorted, so a copy of 3 can only sit right behind it. This one is a copy and has to leave the chain.
11233cur3 now points to nothing; cur stays
cur3
nextnone
One pointer change drops the copy. cur does not move, because the node now behind it has not been checked and could be another 3.
11233no next node → return 1 → 2 → 3
return1
cur has nothing behind it, so the loop ends. The faded nodes are no longer reachable from the head, which is how they count as deleted.
05

Common pitfalls

Moving forward after every unlink

✗ Wrong
if cur.val == cur.next.val:
    cur.next = cur.next.next
cur = cur.next
✓ Right
if cur.val == cur.next.val:
    cur.next = cur.next.next
else:
    cur = cur.next

On [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

✗ Wrong
while cur.next:
✓ Right
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).

06

Edge cases

Every node has the same value

cur never moves. Each pass unlinks one more copy until cur.next is null, leaving the single head node.

No repeats at all

Every comparison finds a new value, so cur walks to the last node and nothing is unlinked.

07

Complexity

Time
O(n)
Space
O(1)
Each comparison either unlinks a node or moves cur forward one node, and each of those can happen at most n times. Only one pointer is stored.
08

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.