DIY: Reverse Nodes in k-Group

Solve the interview question "Reverse Nodes in k-Group", in this lesson.

Problem statement

Given a linked list, you will reverse the nodes of the linked list k at a time and return the modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k, then the nodes left in the end will remain in their original order.

You may not alter the values in the nodes of the list. Only the nodes themselves may be changed.

Note: Use only O(1)O(1) extra memory space.


The input will be a linked list and a positive integer k. Here is an example input:

head = [1,2,3,4,5], k = 2


The output should be the modified list. Here is an example output:


Level up your interview prep. Join Educative to access 70+ hands-on prep courses.