Remove Linked List Elements

Given the head of a linked list and an integer val, remove all the nodes of the linked list that have a value equal to val, and return the new head of the list.

Constraints

  • The number of nodes in the list is in the range [0, 10^4].
  • 1 <= Node.val <= 50
  • 0 <= val <= 50
Auto-saved · 9 lines
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
pass
Console

Click Run to execute sample tests.