Reverse a Linked List in groups of given size.
Medium Accuracy: 45.83% Submissions: 63657 Points: 4

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.

Example 1:

Input:
LinkedList: 1->2->2->4->5->6->7->8
K = 4
Output: 4 2 2 1 8 7 6 5

Example 2:

Input:
LinkedList: 1->2->3->4->5
K = 3
Output: 3 2 1 5 4

Your Task:
The task is to complete the function reverse() which should reverse the linked list in group of size and return the head of the modified linked list.

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

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Output Window