Reverse a Linked List in groups of a given size

Published by Igor Khrupin on


In the previous post, we talked about reverse the entire LinkedList. You also can get info about what is it LinkedList and a high-level view of how it works.

Here is the link:

Today we will talk about a case when we need to revert linked list nodes in groups of a given size.

Algorithm theory

Task definition

Given LinkedList:
Given group size: 3
We need split linked list into groups and reverse the nodes.
The result linked list must be like this:


  1. Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and the pointer to the previous node be prev. See this post for reversing a linked list.
  2. head->next = reverse(next, k) ( Recursively call for rest of the list and link the two sub-lists )
  3. Return prev. Prev becomes the new head of the list

Sample code

Below you will get a link to GitHub repo with Kotlin, Java, and Python source code

Download it from github


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.