Skip to content
Decoder

Remove Duplicates from Sorted List II

Medium

Problem

You get the head of a linked list sorted in ascending order. Delete every node whose value appears more than once, so only values that were unique in the original list are left. Return the head of the result, which stays sorted.

Example 1

Input
1
Output
2 -> 3 -> 3 -> 4 -> 4 -> 5 → 1 -> 2 -> 5

Example 2

Input
1
Output
1 -> 1 -> 2 -> 3 → 2 -> 3

Example 3

Input
1
Output
1 →
Why
empty

Return None (null in TypeScript) if nothing is left.

Constraints

  • 0 <= number of nodes <= 300
  • -100 <= Node.val <= 100
  • The list is sorted in ascending order.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.