Ctrl K

Sorted List, Unique Values Only

Medium
You get a singly linked list starting at head, sorted in non-decreasing order. If a value occurs more than once, delete **all** nodes holding it, without keeping a single copy. Return the head of the resulting list; it stays sorted. The list may be empty.
Example 1
head=list_node([-3, 0, 0, 2, 7, 7, 7, 9]) → [-3, 2, 9]

The values 0 and 7 repeat, so all their nodes are removed. What remains is -3 → 2 → 9.

Example 2
head=list_node([4, 4]) → None

The only value repeats, so the list becomes empty.

Example 3
head=list_node([1, 5, 6]) → [1, 5, 6]
Constraints
  • 0 ≤ number of nodes ≤ 300
  • -100 ≤ Node.val ≤ 100
  • The list is sorted in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDrop the K-th Node From the Tail MediumMove the Tail to the Front MediumSplit a List Around a Pivot MediumList Folded From Both Ends MediumSorting a Linked List Medium
loading Python…
Input
head=list_node([-3, 0, 0, 2, 7, 7, 7, 9])
Expected
[-3, 2, 9]

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.