
The given linked list is -

If K = 3, then the list after rotating it by K nodes is:

1. The value of any node in the linked list will not be equal to -1.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains 'N' space-separated integers representing the value of each node. -1 at the end denotes NULL pointer.
The second line of each test case contains a single positive integer 'K'.
For each test case, in a separate line, print the final rotated linked list.
You don’t have to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 3000
1 <= K < N
-10 ^ 7 <= data <= 10 ^ 7
where ‘T’ is the total number of test cases, 'N' is the total number of nodes in the linked list and ‘data’ is the value of any node of the linked list.
The idea is to use an extra list to first store the last N - K elements then store the starting K elements.
The idea is to change the link of the pointers after the Kth node. We need to change next of Kth node to NULL next of last node to previous head node, and prev of head node to last node and finally change head to (K+1)th node and previous of new head node to NULL.