Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Reverse a Linked List in groups of given size ‘K’

Objec­tive: Given a linked list and inte­ger ‘k’, write an algo­rithm to reverse the linked list in groups of size ‘k’.

Exam­ple:

Reverse a Linked List in groups of given size 'K' Example

Reverse a Linked List in groups of given size ‘K’ Example

Approach:

  • Ear­lier we have seen how to reverse a linked list, solu­tion for reverse the linked list in groups of size will be exten­sion of this solution.
  • Reverse first ‘k’ nodes of the linked list, the kth node will be a new head, return it.
  • Make a recur­sive call to rest of the list and attach it to the last node.(See the pic­ture below)
Reverse a Linked List in groups of given size 'K'

Reverse a Linked List in groups of given size ‘K’

Com­plete Code:

Output:

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

You may also like...