Timer is Running

Given a linked list of size N. The task is to reverse every k nodes (where k is an input to the function) in the linked list.

First line of input contains number of testcases T. For each testcase, first line contains length of linked list and next line contains the linked list elements.

For each testcase, there will be a single line of output which contains the linked list with every k group elements reversed.

User Task:
The task is to complete the function reverse() which should reverse the linked list in group of size k.

Expected Time Complexity : O(n)
Expected Auxilliary Space : O(1)


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

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

Testcase 1:
Since, k = 4. So, we have to reverse every group of four elements. Modified linked list is as 4, 2, 2, 1, 8, 7, 6, 5.


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

Contributor: Harshit Sidhwa
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 ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.