Skip to the code
cf.
+10 XP

#224 Drop Dismissed Toasts

easy

A notification center keeps its toast messages in a linked list. Each node's val is the id of the toast's type. When the user clicks "dismiss all" on one type, every toast of that type must go.

Write dropToasts(head, type) that removes every node whose val equals type and returns the head of the list that is left. Keep the other nodes in their original order. If every node is removed, return null.

Example 1

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

Both nodes holding 6 are removed; the rest keep their order.

Example 2

Input: head = [7,7,7,7], type = 7
Output: []

Every toast is of type 7, so nothing is left.

Constraints

  • The list has 0 to 10^4 nodes.
  • 1 <= node.val, type <= 50

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).