Last Updated: 24 Dec, 2020

Reverse List In K Groups

Hard
Asked in companies
SAP LabsSamsungIBM

Problem statement

You are given a linked list of 'n' nodes and an integer 'k', where 'k' is less than or equal to 'n'.


Your task is to reverse the order of each group of 'k' consecutive nodes, if 'n' is not divisible by 'k', then the last group of nodes should remain unchanged.


For example, if the linked list is 1->2->3->4->5, and 'k' is 3, we have to reverse the first three elements, and leave the last two elements unchanged. Thus, the final linked list being 3->2->1->4->5.


Implement a function that performs this reversal, and returns the head of the modified linked list.


Example:
Input: 'list' = [1, 2, 3, 4], 'k' = 2

Output: 2 1 4 3

Explanation:
We have to reverse the given list 'k' at a time, which is 2 in this case. So we reverse the first 2 elements then the next 2 elements, giving us 2->1->4->3.


Note:
All the node values will be distinct.


Input Format:
The first line of the input contains a single integer 'n', denoting the number of nodes in the linked list.

The second line contains 'n' space-separated integers, denoting the elements of the linked list.

The third line of input contains an integer 'k'.


Output Format:
Return the head of the modified linked list.


Note:
You don't need to print anything, just implement the given function. Contents of your returned linked list will be printed in a single line.


Approaches

01 Approach

  • We can use recursion to solve this problem.
  • The main idea is to reverse the first ‘K’ nodes by ourselves and let the recursive function reverse the rest of the linked list.
  • Let reverseList() be the recursive function which takes the head of the linked list and ‘K’ as the parameters.
  • Now we can iteratively reverse the first ‘K’ nodes of the linked list.
  • And then if we still have some nodes left in the linked list, then we can call reverseList(), until the nodes are exhausted.

02 Approach

  • Since we have to optimize on space, instead of using recursion we can use some pointers to create the links between the different sets of 'K' nodes.
  • First of all we have to create some pointers to keep the track of a current set of ‘K’ nodes and the previously modified linked list and curr denotes the current node that we are on.
  • Let ‘prev’ denote the previous pointer, a ‘tail’ pointer which keeps the track of the last node of the ‘K’ set of nodes and ‘join’ pointer points to the current set of nodes which we have to join to the modified linked list.
  • Now keep on traversing the list until we reach the end and do the following:
    • Let ‘join’ = ‘curr’ and ‘prev’ = NULL. Now reverse the ‘K’ set of nodes in the linked list.
    • If we have not assigned the ‘newHead’, then assign the ‘newHead’ to the ‘prev’ pointer.
    • If we already have the tail of the linked list, then the next of tail should point to the ‘prev’ i.e. ‘tail’ -> ‘next’ = ‘prev’.
    • Now update the ‘tail’ to the last node of the K-reversed linked list i.e. ‘tail’ = ‘join’.
  • For example, let the linked list be 1 2 3 4 and k = 2.
  • Initially, we will have ‘curr’ pointing to the ‘head’ of the list. Until we don’t reach the end, we do the following steps:
    • Setting ‘join’ = ‘curr’ and ‘prev’ = NULL and hence join will point to 1 initially. After reversing the first ‘K’ nodes, our prev will point to 2.
    • Now we will assign the tail to join because the join was initially pointing to the head and after reversing the ‘K’ nodes it will be pointing to the last node of the current set of nodes.
    • We will repeat the same process again and hence we will get the required output.
       
  • Lastly, If the last group has less than ‘K' nodes, we will reverse it again to get the original order.