


The first line of the input contains an integer ‘T’ representing the number of test cases.
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.
For each test case, return the head of the linked-list denoting the values of nodes of the singly linked list after removing all consecutive sequences that sum to 0.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
0 <= N <= 100
-10 ^ 3 <= data <= 10 ^ 3 and data != -1
Where 'N' denotes the number of nodes in the given Linked List.
Time Limit: 1 sec
The idea here is to iterate over all the nodes in the linked list from left to right, and for each node, we will find the sum of all consecutive sequences of nodes that start with this node. Among all the consecutive sequences that start with the current node, we will remove that sequence of nodes that sums to 0 and occur first in the given linked list.
The idea here is to scan the linked list from left to right and calculate the prefix sum. We make a hashmap that takes the prefix sum as a key and the corresponding node as the value. Whenever we encounter a repeated prefix sum, we find the node from the hashmap where this prefix sum occurred, say startNode, and remove all consecutive nodes from the next of startNode till the current node since they sum to 0. For
Example:
Consider that you have this Linked list: [7, 2, -2, 2, 3, -5]

Let’s calculate the prefix sum corresponding to each node in this list

Now we will iterate through the list from left to right and when we encounter a repeated prefix sum we will remove all consecutive nodes between them including the current node. As shown below:

We will repeat this process until there are no such consecutive sequences left.