#K75187. Remove Elements from Linked List
Remove Elements from Linked List
Remove Elements from Linked List
You are given a singly linked list and an integer value \(val\). Your task is to remove all the nodes from the linked list that have a value equal to \(val\), and then return the head of the modified linked list.
The linked list is defined as:
class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }
You need to implement a function (or its equivalent in your language) that performs this removal operation.
Note: If the resulting linked list is empty, output nothing.
inputFormat
The input is given via stdin in the following format:
- An integer \(n\) representing the number of nodes in the linked list.
- A line with \(n\) space-separated integers denoting the values of the nodes. If \(n = 0\), this line will be empty.
- An integer \(val\) representing the value to be removed from the list.
For example:
7 1 2 6 3 4 5 6 6
outputFormat
The output should be printed to stdout. Print the values of the modified linked list in order, separated by a single space. If the list is empty, output nothing.
For example, the output for the sample input above should be:
1 2 3 4 5## sample
7
1 2 6 3 4 5 6
6
1 2 3 4 5