144. Remove Duplicates from Sorted List II

Medium · Linked List

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers. Return the head of the modified list.

A sorted linked list means values are in non-decreasing order. You must remove entire groups of duplicate values, not just reduce them to single occurrences.

Examples

Example 1
Input: [1, 2, 2, 3]
Output: [1, 3]
Explanation: The value 2 appears twice, so both nodes with value 2 are removed. Only 1 and 3 remain.
Example 2
Input: [1, 1, 1, 2, 3]
Output: [2, 3]
Explanation: The value 1 appears three times, so all three nodes are removed. Only 2 and 3 remain.

Constraints