MediumLinked ListTwo PointersRecursion

1 approach, code in every language

Problem description

You are given the head of a sorted singly linked list. Remove every node whose value appears more than once, keeping only the values that are unique across the entire list.

Unlike the simpler variant that keeps one copy of each value, here a value that repeats is deleted completely. The remaining nodes must stay in their original sorted order. Return the head of the resulting list.

Example 1

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

Output: [1,2,5]

The values 3 and 4 each occur twice, so all of their nodes are removed, leaving only 1, 2, and 5.

Example 2

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

Output: [2,3]

The value 1 repeats three times and is dropped entirely, while 2 and 3 appear once and survive.

Constraints

  • The number of nodes in the list is in the range [0, 300].
  • -100 <= Node.val <= 100
  • The list is guaranteed to be sorted in non-decreasing order.

After the pattern

Now solve one you haven't seen.

Unique Scan Trim is our own problem on the technique behind Remove Duplicates from Sorted List II: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free