Remove Linked List Elements

Easy

Company Tags

You are 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:

Input: head = [2,1,4,1,2,3], val = 2

Output: [1,4,1,3]

Example 2:

Input: head = [1,1], val = 1

Output: []

Constraints:

  • 0 <= Length of the list <= 10,000.
  • 1 <= Node.val <= 50
  • 0 <= val <= 50


Company Tags

Please upgrade to NeetCode Pro to view company tags.

head =

val =