Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Problem 2: Remove Duplicates from Sorted List (easy)
On this page

Problem Statement:

Examples

Example 1:

Example 2:

Example 3:

Try it yourself

Problem Statement:

Given a sorted linked list, remove all the duplicate elements to leave only distinct numbers. The linked list should remain sorted, and the modified list should be returned.

Examples

Example 1:

  • Input: 1 -> 1 -> 2
  • Output: 1 -> 2
  • Justification: Since 1 is repeated, we remove the duplicate to leave a sorted list of unique numbers.

Example 2:

  • Input: 1 -> 2 -> 2 -> 3
  • Output: 1 -> 2 -> 3
  • Justification: Here, 2 is the duplicate element, and by removing it, we obtain a list containing only distinct elements.

Example 3:

  • Input: 3 -> 3 -> 3
  • Output: 3
  • Justification: We remove the repeated 3s to leave a single 3 in the list.

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.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement:

Examples

Example 1:

Example 2:

Example 3:

Try it yourself