Skip to content
Decoder

Remove Linked List Elements

Easy

Problem

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.

Example 1

Input
1
Output
2 -> 6 -> 3 -> 4 -> 5 -> 6, val = 6 → 1 -> 2 -> 3 -> 4 -> 5

Example 2

Input
7
Output
7 -> 7 -> 7, val = 7 →
Why
empty

Example 3

Input
(empty), val = 1
Output
(empty)

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.