Remove Linked List Elements

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

Example 1
[1] -> [2] -> [6] -> [3] -> [4] -> [5] -> [6] -> null
---
[1] -> [2] -> [3] -> [4] -> [5] -> null
Inputhead = [1,2,6,3,4,5,6], val = 6
Output[1,2,3,4,5]
All nodes with value 6 are removed, leaving the remaining nodes in their original order.
Example 2
null
---
null
Inputhead = [], val = 1
Output[]
The list is empty, so there are no nodes to remove.

Constraints

  • The number of nodes in the list is in the range [0, 10^4].
  • 1 <= Node.val <= 50
  • 0 <= val <= 50

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate