


If the first list is: [0 -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL], the second list is: [10 -> 20 -> 30 -> NULL], ‘X’ = 2 and ‘Y’ = 4 then if we remove nodes from index 2 to 4, and after inserting the second list at index 2 then the final list will be: [0 -> 1 -> 10 -> 20 -> 30 -> 5 -> NULL].
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains two space-separated integers denoting the value of ‘X’ and ‘Y’.
The second line of each test case contains space-separated integers denoting the values of the first Linked List nodes. The Linked List is terminated with -1.
The third line of each test case contains space-separated integers denoting the values of the second Linked List nodes. The Linked List is terminated with -1.
For each test case, print the final linked list in a single line in a space-separated manner.
Print the output of each test case in a separate line.
1 <= T <= 10
3 <= N <= 10 ^ 6
1 <= M <= 10 ^ 6
-10^9 <= Node.Data <= 10^9 and Node.Data != -1
1 <= X <= Y < N - 1
The linked list is 0-indexed.
Time Limit: 1 sec
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
In this approach, we will use two pointers, prev1 and prev2. We will set prev1 to the node at index (X - 1) in the first Linked LIst and prev2 to the node at index Y in the first Linked List. We will remove the link between nodes at index X - 1 and X and between nodes at index Y and Y - 1 by creating a link between prev1 with the head of the second Linked List(prev2 -> head2) and the tail of the second linked list with the prev2.next(tail.next -> prev2.next). Thus, removing the first Linked List’s nodes from index X to Y and putting the second list in their place.
Algorithm: