Frontend School

Remove Duplicates from Sorted Linked List

DSA
easy

Remove Duplicates from Sorted Linked List

Easy
Linked List
Data Cleaning

Overview

Innovaccer deals with vast amounts of patient data, often received from various sources. This data needs to be cleaned and standardized for accurate analysis. One common issue is redundant entries, especially when data is aggregated from pre-sorted feeds. Your task is to implement a function that removes duplicate nodes from a sorted singly linked list, ensuring each value appears only once. This is crucial for maintaining data integrity and optimizing storage/processing efficiency in our data pipelines.

What you need to do / Task

Implement a function deleteDuplicates that takes the head of a sorted singly linked list as input. The function should remove all duplicate nodes such that each element appears only once. The resulting list should also be sorted.

Input & output

  • Input: head: A ListNode representing the head of a sorted singly linked list. Each ListNode has an integer val and a next pointer to the next node.
  • Output: A ListNode representing the head of the modified sorted singly linked list with duplicates removed.

Clarifications

  • The list is guaranteed to be sorted in non-decreasing order.
  • The list can be empty or contain only one node.
  • The val of nodes will be within standard integer limits.

Examples

1

Example 1

Input:

head = [1,1,2]

Output:

[1,2]

Note:

The original list is 1 -> 1 -> 2. The duplicate '1' is removed, resulting in 1 -> 2.

2

Example 2

Input:

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

Output:

[1,2,3]

Note:

The original list is 1 -> 1 -> 2 -> 3 -> 3. Duplicate '1' and '3' are removed, resulting in 1 -> 2 -> 3.

3

Example 3

Input:

head = [1,2,3]

Output:

[1,2,3]

Note:

The list contains no duplicates, so it remains unchanged.

4

Example 4

Input:

head = []

Output:

[]

Note:

An empty list remains empty.

Constraints

Constraints & assumptions

  • The number of nodes in the list is between 0 and 300.
  • -100 <= Node.val <= 100.
  • The list is guaranteed to be sorted in ascending order.

What interviewers evaluate

  • Correctness of the algorithm for removing duplicates while maintaining sorted order.
  • Ability to handle edge cases like empty lists or lists with no duplicates.
  • Efficient traversal and manipulation of linked list pointers.
  • Clean and readable code structure.

Loading editor...