dsa · easy

Remove Duplicates from a Sorted List

You are given head, the node values of a singly linked list in order from the front. The values are already sorted non-decreasing. Collapse every run of equal neighbours so each distinct value appears once, keeping the original relative order. Return the remaining values.

Arguments

An empty list stays empty.

Example

head = [2,2,2,5,5,9]

The three 2s shrink to one 2, the two 5s shrink to one 5, and 9 is already unique, so the result is [2,5,9].

[4] has nothing to collapse → [4].

Constraints

0 <= head.length <= 300 -100 <= head[i] <= 100 head is sorted in non-decreasing order Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[2,2,2,5,5,9]

Expected:
[2,5,9]

Example 2

Input:
[4]

Expected:
[4]

Example 3

Input:
[]

Expected:
[]

Open in the Dojo editor