Ctrl K

Cut Out Matching Nodes

Easy
You are given a singly linked list head and a number target. Remove every node whose value equals target; the other nodes keep their order. Return the head of the resulting list. It may change if the first nodes are removed, and if everything is removed, return an empty list.
Example 1
head=list_node([3, 8, 3, 1, 3]), target=3 → [8, 1]

All three nodes holding 3 are cut out, including the first and the last; 8 and 1 remain.

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

Every node goes, leaving an empty list.

Example 3
head=list_node([2, 4, 6]), target=9 → [2, 4, 6]

No node holds 9, so the list stays the same.

Constraints
  • The list has between 0 and 10⁴ nodes
  • 1 ≤ Node.val ≤ 50
  • 0 ≤ target ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZip Two Sorted Chains EasyFlip the Chain EasySymmetric Bead String EasyAdding Digit Lists MediumTrade Places in Twos Medium
loading Python…
Input
head=list_node([3, 8, 3, 1, 3]), target=3
Expected
[8, 1]

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