SDE & MLE Prep

83. Remove Duplicates from Sorted List

Problem

The linked list is sorted. Delete duplicates so each value appears exactly once, and return the head.

Example: 1→1→2→3→3 → 1→2→3

Sorted → duplicates are adjacent. Skip cur.next while it equals cur.

Complexity: O(n).

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = head
        while cur and cur.next:
            if cur.next.val == cur.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head