Remove duplicate values from a sorted singly linked list so each value appears only once.
Problem
You are given the head of a sorted singly linked list. Remove all duplicate nodes so that each value appears exactly once, while preserving the original relative order of the remaining nodes.
The linked list is sorted in non-decreasing order, so any duplicate values will appear next to each other.
Return the head of the modified list.
Notes
- You should modify the list in place when possible.
- Only duplicate nodes need to be removed; keep one node for each distinct value.
Intuition check
Because the list is already sorted, duplicates are adjacent. That allows a single pass through the list without extra storage.
Input Format
- The input is the head of a singly linked list.
- Each node contains an integer value.
- The list is sorted in non-decreasing order.
Output Format
- Return the head of the linked list after removing duplicate nodes.
Constraints
- The list may be empty.
- Node values are integers.
- The list is sorted in non-decreasing order.
- Aim for time and extra space, excluding the input list itself.
Example 1
Input
head = [1,1,2]
Output
[1,2]
Explanation
The two leading 1s are duplicates, so keep one and remove the other.
Example 2
Input
head = [1,1,2,3,3]
Output
[1,2,3]
Explanation
Remove repeated adjacent values and keep a single node for each number.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.