308. Remove Duplicates from Sorted Linked List

EasyLinked ListLinked ListSortedDuplicates

Given the head of a sorted singly linked list, delete all duplicates such that each element appears only once. Return the head of the sorted linked list.

The list is guaranteed to be sorted in non-decreasing order.

Input: Head of a sorted singly linked list.

Output: Head of the deduplicated sorted linked list.

Examples

Example 1
Input: [1,1,2]
Output: [1,2]
Explanation: node(1)→node(1)→node(2): skip second 1. node(1).next = node(2). Return [1,2].
Example 2
Input: [1,1,2,3,3]
Output: [1,2,3]
Explanation: Skip duplicate 1 and duplicate 3. Result: [1,2,3].
Example 3
Input: [1,2,3]
Output: [1,2,3]
Explanation: No duplicates. Return list unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →