Given a singly linked list of size N. The task is to rotate the linked list counter-clockwise by k nodes, where k is a given positive integer smaller than or equal to length of the linked list.

Input Format:
First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list and next line contains linked list elements and last line contains k, by which linked list is to be rotated.

Output Format:
For each testcase, print the rotated linked list.

User Task:
The task is to complete the function rotate() which takes head reference as the first argument and k as the second argument. The printing is done automatically by the driver code.

1 <= T <= 100
1 <= N <= 103
1 <= k <= 103

1 2 3 4 5 6 7 8
2 4 7 8 9

1 2

5 6 7 8 1 2 3 4
8 9 2 4 7
1 2

Testcase 1:
After rotating the linked list by 4 elements (anti-clockwise), we reached to node 5, which is (k+1)th node from beginning, now becomes head and tail of the linked list is joined to the previous head.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa,Saksham Raj Seth
Author: Karan Grover

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.