Reverse a Linked List in groups of given size

Given a linked list, write a function to reverse every k nodes (where k is an input to the function).

Example:
Inputs:  1->2->3->4->5->6->7->8->NULL and k = 3 
Output:  3->2->1->6->5->4->8->7->NULL. 

Solution : Reverse a LinkedList keeping counter, to keep node count along with it.


Algo :


  1. Cur = Head, Next = Null, Pre = Null
  2. While (Curr != NULL AND Counter < k ) //till the end & counter to keep less than input k
    1. next = cur -> nextt
    2. cur-> nextt = pre 
    3. pre = cur
    4. cur = next
    5. count ++;
  3. if ( next !=  NULL)
    1.  head->next = reverse(next, k); 
  4. return prev;
Step 2  : is to reverse a linked list. by keeping K set we are reversing only first K node.
Step 3  : is to reverse next k node, 
so till the end of linked list we are reversing each k nodes.
Step 4  : prev will be head of this linkedlist, return it.