Ctrl K

Keep Only One-Off Values in a List

Medium
You get a singly linked list starting at head; its values come in no particular order. Find every value that occurs more than once in the list and delete **all** nodes holding such values. The remaining nodes keep their order. Return the head of the resulting list (an empty list if no nodes are left).
Example 1
head=list_node([4, 7, 4, 9, 1]) → [7, 9, 1]

The value 4 occurs twice, so both nodes holding 4 are removed. What remains is 7 → 9 → 1.

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

Every value repeats, so the list becomes empty.

Example 3
head=list_node([8, 2, 6]) → [8, 2, 6]
Constraints
  • 1 ≤ number of nodes ≤ 10⁵
  • 1 ≤ Node.val ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMarked Runs in a Chain MediumCancel Zero-Sum Stretches MediumPurge Banned Values MediumHow Often Each Value Appears EasyAdding Digit Lists Medium
loading Python…
Input
head=list_node([4, 7, 4, 9, 1])
Expected
[7, 9, 1]

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