Remove every node whose value matches a given target from a singly linked list.
Given the head of a singly linked list and an integer val, remove every node in the list whose value equals val.
Return the head of the modified list.
You should remove all matching nodes and keep the relative order of the remaining nodes.
head: the head node of a singly linked listval: an integer target valueThe linked list node structure typically contains:
clike
Node {
int value
Node next
}val.Example 1
Input
head = [1,2,6,3,4,5,6], val = 6
Output
[1,2,3,4,5]
Explanation
Both nodes with value 6 are removed, including one at the head-side portion and one at the tail.
Example 2
Input
head = [7,7,7,7], val = 7
Output
[]
Explanation
Every node matches the target, so the resulting list is empty.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.