Skip to the code
cf.
+25 XP

#226 Keep Only One-Off Codes

medium

An alerting tool wants to highlight error codes that happened exactly once, since repeated codes are already known issues. The codes come as a linked list sorted from smallest to largest.

Write keepOneOffs(head) that removes every node whose value appears more than once (all of its copies, not just the extras), and returns the head of what is left. If nothing is left, return null.

Example 1

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

3 and 4 repeat, so every copy of them goes.

Example 2

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

All three 1s go, even though they start the list.

Constraints

  • The list has 0 to 300 nodes.
  • -100 <= node.val <= 100
  • The list is sorted ascending.

Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).