Remove Duplicate Values From a Sorted Linked List
Implement removeDuplicatesFromSorted
Given the
head of a sorted singly linked list, collapse every run of equal values down to a single node, and return the resulting (still sorted) list.
Since the list arrives sorted, any two nodes holding the same value are guaranteed to sit right next to each other — there's no need to search the whole list for matches. The optimal solution uses this adjacent-duplicate checkAdjacent-Duplicate CheckWhen a sequence is sorted, every group of equal values forms one contiguous run. Detecting a duplicate then only requires comparing each element to its immediate neighbor, rather than checking it against everything seen so far.: one pointer walks the list, and whenever the very next node repeats the current value, it's unlinked in place — no extra memory, no full re-scan.
Example 1:
Input: head = [1,1,2,3,3]
Output: [1,2,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1,1,1]
Output: [1]
+ 5 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes in head ≤ 300 - ●
-100 ≤ node value ≤ 100 - ●
head is sorted in non-decreasing order
head =
[1, 1, 2, 3, 3]