Remove All Duplicates from Sorted Linked List - aloalgo

Remove All Duplicates from Sorted Linked List

Medium

You are given the head of a sorted linked list.

Your task is to delete all nodes that have duplicate values, leaving only distinct numbers from the original list.

You may assume that:

  • The linked list is sorted in non-decreasing order.

Return the modified linked list, which should still be sorted.

Example 1

Input
1 → 2 → 3 → 3 → 4 → 4 → 5 → null
Output
1 → 2 → 5 → null
Explanation:

The values 3 and 4 each appear more than once, so all of their nodes are removed. Only 1, 2, and 5 remain.

Example 2

Input
1 → 1 → 1 → 2 → 3 → null
Output
2 → 3 → null
Explanation:

The value 1 appears three times, so all nodes with value 1 are removed. Only 2 and 3 remain.

Example 3

Input
1 → 1 → 2 → 2 → null
Output
None
Explanation:

Both values 1 and 2 have duplicates. All nodes are removed, resulting in an empty list.

Loading...
Input
1 → 2 → 3 → 3 → 4 → 4 → 5 → null
Output
1 → 2 → 5 → null

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!