
Leetcode Problem 82: Remove Duplicates from Sorted List II
Given the head of a sorted linked list, delete all nodes that have duplicate values (keeping no copies), and return the head of the cleaned-up list.
Constraints:
import { ListNode } from "../list-node"
function deleteDuplicates(head: ListNode | null): ListNode | null {
if (!head) {
return null
}
let current: ListNode | null = new ListNode(-1, head)
while (current) {
if (current.next && current.next?.val === current.next?.next?.val) {
let value = current.next.val
let toBeRemoved: ListNode | null = current.next
while (toBeRemoved?.val === value) {
if (toBeRemoved === head) {
head = head.next
}
toBeRemoved = toBeRemoved.next
}
current.next = toBeRemoved
} else {
current = current.next
}
}
return head
}