Remove Linked List Elements
Problem statement
Given the head of a singly linked list and an integer val, remove every node whose value equals val.
Return the head of the remaining list. Reuse the nodes that are not removed.
Function
removeElements(head: ListNode, val: int) → ListNodeExamples
Example 1
head = [1,2,6,3,4,5,6]val = 6return = [1,2,3,4,5]Both nodes containing 6 are removed.
Example 2
head = []val = 1return = []An empty list stays empty.
Example 3
head = [7,7,7,7]val = 7return = []Removing every node yields an empty list.
Constraints
- The list contains between
0and100000nodes. -1000000000 <= Node.val, val <= 1000000000.