Remove Duplicates from Sorted List II

Medium

Description

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

Examples

Input:head = [1,2,3,3,4,4,5]
Output:[1,2,5]
Explanation:

Remove all duplicated values.

Input:head = [1,1,2,2,3,3]
Output:[]
Explanation:

All nodes have duplicates, so they are all removed, resulting in an empty list.

Input:head = [-1,-1,0,1,1,2]
Output:[0,2]
Explanation:

The values -1 and 1 appear twice each, so all instances of these values are removed. Only 0 and 2 appear once, so they remain in the output.

Constraints

  • Number of nodes is in range [0, 300]
  • -100 ≤ Node.val ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!