Skip to main content

Remove Duplicates from Sorted List II

MediumLinked ListTwo PointersArrayBinary SearchMathSorting

Description

Given the head of a sorted linked list, remove every value that appears more than once. Return the head of the filtered linked list. The runner displays linked-list inputs and outputs using array notation.

Examples

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

The values 3 and 4 are duplicated, so every node containing either value is removed.

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 and sharpen your skills for technical interviews.