Remove Duplicates from Sorted List
Remove Duplicates from Sorted List is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Linked List Manipulation Patterns pattern, in the In-place Reversal subpattern.
Problem statement
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Example 1:
Input: head = [1,1,2] Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3] Output: [1,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.
Topics and companies
Topics: Linked List.
Reported in interviews at Amazon, Adobe, Goldman Sachs, Qualcomm.
How to practise Remove Duplicates from Sorted List on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Remove Duplicates from Sorted List in the code editor, or read the Remove Duplicates from Sorted List editorial for a worked solution with its approach and complexity analysis.
Where Remove Duplicates from Sorted List sits in the DSA pattern sheet
- Linked List Manipulation Patterns pattern guide
- In-place Reversal subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Remove Duplicates from Sorted List
Other problems that use the same In-place Reversal and Linked List Manipulation Patterns techniques:
- Palindrome Linked List — Easy, same subpattern
- Remove Duplicates from Sorted List II — Medium, same subpattern
- Reverse Linked List — Easy, same subpattern
- Reverse Linked List II — Medium, same subpattern
- Reverse Nodes in k-Group — Hard, same subpattern
- Add Two Numbers — Medium, same pattern
Preparing your workspace
Progress is saved as you study.