EasyLinked List

1 approach, code in every language

Problem description

You are given the head of a linked list that is already sorted in non-decreasing order. Remove every duplicate value so that each distinct number appears only once.

The list must remain sorted after the removals. Return the head of the modified list.

Example 1

Input: head = [1,1,2]

Output: [1,2]

The second occurrence of 1 is unlinked, leaving one node per distinct value.

Example 2

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

Output: [1,2,3]

Both runs of duplicates (the pair of 1s and the pair of 3s) collapse to a single node each.

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.

Trim Repeat Holds is our own problem on the technique behind Remove Duplicates from Sorted List: 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