Remove All Nodes With Duplicate Values From a Sorted List

Implement removeAllDuplicateNodes

Given the head of a sorted singly linked list, remove every node whose value appears more than once, leaving only values that occur exactly once — the result must stay sorted. This is a stricter cousin of collapsing duplicates: instead of keeping one representative per repeated value, the entire run disappears. The optimal solution walks the list with a dummy node + prev/curr pairDummy Node + Prev/Curr PairA dummy node placed before head lets the very first node be unlinked the same way as any other, without special-casing "head might change." prev always trails the last node confirmed unique, so a discovered run can be skipped by pointing prev.next straight past it.: whenever curr's value matches the value right after it, the entire matching run — however long — is skipped in one relink; otherwise prev simply advances alongside curr.

Example 1:

Input: head = [1,2,3,3,4,4,5]

Output: [1,2,5]

Example 2:

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

Output: [2,3]

Example 3:

Input: head = []

Output: []

+ 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, 2, 3, 3, 4, 4, 5]