EasyLinked List

Remove Duplicates from Sorted List

LeetCode
1 approach, code in all languages

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.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now