Skip to content

Remove Linked List Elements

EasyLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

You get the head of a linked list and a number val. Remove every node whose value equals val and return the head of what is left. The head itself may need to go, possibly several times in a row.

1 -> 2 -> 6 -> 3 -> 4 -> 5 -> 6, val = 6   →  1 -> 2 -> 3 -> 4 -> 5
7 -> 7 -> 7 -> 7, val = 7                  →  (empty)
(empty), val = 1                           →  (empty)

Return None (null in TypeScript) for an empty result.

Constraints

  • 0 <= number of nodes <= 10^4
  • 1 <= Node.val <= 50
  • 0 <= val <= 50