Leetcode•Jul 15, 2025
Remove Duplicates from Sorted List
Hazrat Ali
Leetcode
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]
Solution
/**
* @param {ListNode} head
* @return {ListNode}
*/
const deleteDuplicates = head => {
let slow = head;
let fast = head;
while (fast) {
while (fast && fast.val === slow.val) {
fast = fast.next;
}
slow.next = fast;
slow = fast;
}
return head;
};