ExamAdda Logo

Remove Duplicates from Sorted List II

Easy

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Return the linked list after removing all duplicates.

Example 1

Input

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

Output

[1,2,5]

Explanation

3 and 4 appear more than once, so all nodes containing those values are removed.

Example 2

Input

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

Output

[2,3]

Explanation

All occurrences of 1 are duplicates, so they are completely removed. The remaining list is [2,3].

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 ascending order.

Hints:

Hint 1

Use a dummy node before the head so that duplicates at the beginning can be removed easily.

Hint 2

When consecutive nodes have the same value, skip all nodes with that value instead of keeping one occurrence.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 18, 2026

Last updated

Aug 19, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output