Remove Duplicates from Sorted List

Given the head of a sorted linked list, delete all duplicates such that each element appears only once and return the updated head. Example: Input: head = [1,1,2] Output: [1,2] Explanation: We remove the extra '1' from the list. Pattern focus: Dummy Head Node technique (simplifies deletions).

Input Format

head = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes in the list is in the range [0, 10^4].
  • -100 <= Node.val <= 100

Examples

Example 1:

Input:

head = [1,1,2]

Output:

[1,2]

Explanation:

Remove the extra 1 to leave [1,2].

Example 2:

Input:

head = [1,1,2,3,3]

Output:

[1,2,3]

Explanation:

Duplicate 1 and 3 are removed.

Loading...
Remove Duplicates from Sorted List - Linked List