You are given a Singly Linked List of integers and an integer 'K'. Your task is to remove all such nodes from the linked list whose value is equal to 'K'.
A singly linked list is a linear data structure in which we can traverse only in one direction i.e., from Head to Tail. It consists of several nodes where each node contains some data and a reference to the next node.
A sample Linked List-

The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains space-separated integers denoting the values of nodes of the Linked List. The Linked List is terminated with -1. Hence, -1 is never a node value of the Linked List.
The second line of each test case contains a single integer 'K' which denotes the value of the nodes to be removed.
Output format:
For each test case, print the elements of the resultant Linked List after removing all nodes having the value 'K'.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
0 <= node.data <= 10^9 and node.data != -1
0 <= K <= 10^9
Where 'node.data' denotes the value of a Linked list node, and 'K' denotes the value of the nodes to be removed.
Time limit: 1 sec
2
2 5 7 10 -1
7
4 9 10 -1
3
2 5 10 -1
4 9 10 -1
For the first test case, the given Linked List is
So, after deleting the node with value 7, the Linked List becomes
For the second test case, the given Linked List is
As no node in the Linked List has value 3, the Linked List remains unchanged.
2
4 9 4 -1
4
1 2 -1
6
9 -1
1 2 -1
For the first test case, the given Linked List is [4, 9, 4]. So, after deleting both the node with value 4, the Linked List becomes [9].
For the second test case, the given Linked List is [1, 2]. As no node in the Linked List has value 6, the Linked List remains unchanged.
Try to solve the current node and pass the other problem as sub problem.
Approach: The idea is to use recursion to solve the problem. The working of the recursive function is explained below. The basic idea is pretty simple that if a node has value K, then we will remove this node from the Linked List, and our output will be the node returned by the recursive call for its next node. Otherwise, our output will be the current node itself.
Working of the recursive function:
O(N), where N is the number of nodes in the Linked List.
The recursive function is called for each node of the Linked List only once. Hence, the overall Time Complexity is O(N).
O(N), where N is the number of nodes in the Linked List.
As we are using recursive approach and it uses stack to store all the recursive calls. Thus, maximum number of calls can be upto N ans So, space complexity is O(N).