Remove Duplicates From Sorted List

Easy Topics Company Tags

The singly linked list starting at head is sorted in nondecreasing order. Remove duplicate nodes so that each distinct value remains exactly once, and return the head of the resulting list. Preserve the sorted order. In the examples and test inputs, an array lists node values from head to tail; [] represents an empty list.

Example 1:

Remove Duplicates from Sorted List, example 1: illustration of the example

Input: head = [0,0,1,2,2,2]

Output: [0,1,2]

Explanation: Keep one node with value 0, the node with value 1, and one node with value 2. The resulting chain is 0 -> 1 -> 2.

Example 2:

Remove Duplicates from Sorted List, example 2: illustration of the example

Input: head = [5,5,5]

Output: [5]

Explanation: All three nodes have the same value. Keep one node with value 5.

Example 3:

Remove Duplicates from Sorted List, example 3: illustration of the example

Input: head = []

Output: []

Explanation: The input list is empty, so there are no nodes to remove and the result is empty.

Constraints:

  • The list contains between 0 and 300 nodes.
  • -100 <= Node.val <= 100
  • Node values are sorted in nondecreasing order.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.