PART 13 | REVERSE NODES IN K-GROUP
Reverse Nodes in K-Group in Java — Complete Pointer-by-Pointer Explanation
Reversing an entire Linked List is straightforward once you understand:
prev
curr
nextBut what if we only want to reverse the list in groups of K nodes?
For example:
1 → 2 → 3 → 4 → 5 → 6If:
k = 3we reverse every group of 3 nodes:
1 → 2 → 3becomes:
3 → 2 → 1and:
4 → 5 → 6becomes:
6 → 5 → 4Final result:
3 → 2 → 1 → 6 → 5 → 4This problem is one of the best exercises for mastering Linked List pointer manipulation.
What You’ll Learn
| # | Topic |
| -: | ---------------------- |
| 1 | Problem |
| 2 | Example |
| 3 | Understand Groups |
| 4 | Find Kth Node |
| 5 | Reverse Group |
| 6 | Connect Previous Group |
| 7 | Connect Next Group |
| 8 | Dummy Node |
| 9 | Complete Algorithm |
| 10 | Dry Run |
| 11 | Java Code |
| 12 | Complexity |
| 13 | Common Mistakes |
| 14 | Practice |1. Problem
Given the head of a linked list and an integer k, reverse the nodes of the linked list in groups of k.
Important rule:
If the number of nodes remaining is less than
k, leave those nodes unchanged.
For example:
1 → 2 → 3 → 4 → 5with:
k = 2becomes:
2 → 1 → 4 → 3 → 5The last node remains unchanged because there is only one node left.
2. Example
Input
1 → 2 → 3 → 4 → 5 → 6k = 3Divide the list:
Group 1:1 → 2 → 3Group 2:4 → 5 → 6Reverse each group:
3 → 2 → 16 → 5 → 4Final:
3 → 2 → 1 → 6 → 5 → 43. Understand the Groups
Before writing code, understand exactly what a group looks like.
Consider:
1 → 2 → 3 → 4 → 5 → 6 → 7For:
k = 3we have:
Group 1:
1 → 2 → 3Group 2:
4 → 5 → 6Remaining:
7
The first two groups are reversed:
3 → 2 → 16 → 5 → 4The remaining node stays unchanged:
7Final:
3 → 2 → 1 → 6 → 5 → 4 → 7This gives us an important rule:
If group size == k
↓
Reverse it
If group size < k
↓
Leave it unchanged4. Find the Kth Node
Before reversing a group, we need to know where the group ends.
Suppose:
1 → 2 → 3 → 4 → 5 → 6and:
k = 3If the group starts at 1, the kth node is:
3We can use a temporary pointer:
Node kth = groupPrev;
for (int i = 0; i < k; i++) {
kth = kth.next;
}If kth == null, there are not enough nodes for a complete group.
Therefore, we should stop and leave the remaining nodes unchanged.
5. Reverse the Group
Suppose we have:
1 → 2 → 3 → 4and want to reverse:
1 → 2 → 3The important pointers are:
groupPrev
groupStart
groupEnd
groupNextInitially:
groupPrev
↓
dummy → 1 → 2 → 3 → 4
↑ ↑ ↑
groupStart kth nextSave the node after the group:
Node groupNext = kth.next;Now reverse only:
1 → 2 → 3After reversal:
3 → 2 → 1and:
groupNext = 4So we eventually want:
dummy → 3 → 2 → 1 → 46. Connect Previous Group
After reversing a group, we need to connect it to the previous part of the list.
Before reversal:
previous → 1 → 2 → 3 → 4After reversal:
previous → 3 → 2 → 1 → 4The previous group’s last node should point to the new first node.
So:
groupPrev.next = kth;Remember:
kthwas originally the last node of the group.
After reversal, it becomes the first node.
7. Connect Next Group
After reversing:
1 → 2 → 3we get:
3 → 2 → 1But we still need:
1 → nextGroupSo:
groupStart.next = groupNext;Notice that groupStart becomes the tail after reversal.
Therefore:
groupPrev.next = kth;
groupStart.next = groupNext;connects both sides of the reversed group.
8. Dummy Node
A dummy node makes the first group much easier to handle.
Without a dummy node:
1 → 2 → 3 → 4when reversing the first group, the head changes from:
1to:
3We would need special handling.
With a dummy:
dummy → 1 → 2 → 3 → 4we can treat every group in exactly the same way.
At the end:
return dummy.next;9. Complete Algorithm
The complete process is:
Create dummy node
↓
groupPrev = dummy
↓
Find kth node
↓
If kth == null
↓
Stop
↓
Save groupNext
↓
Reverse current group
↓
Connect previous group
↓
Connect next group
↓
Move groupPrev to end of reversed group
↓
RepeatThe most important variables are:
groupPrev
groupStart
kth
groupNext10. Reverse One Group — Pointer Explanation
Suppose:
1 → 2 → 3 → 4and:
k = 3We want:
3 → 2 → 1 → 4Before reversal:
groupPrev
↓
dummy → 1 → 2 → 3 → 4
↑ ↑
start kthSave:
Node groupNext = kth.next;Therefore:
groupNext → 4Now reverse:
1 → 2 → 3using:
Node prev = groupNext;
Node current = groupPrev.next;This is an important trick.
We initialize:
prev = groupNextinstead of:
prev = nullbecause the reversed group’s tail should point directly to the next group.
11. Java Code
Here is the complete optimized solution.
public static Node reverseKGroup(Node head, int k) {
if (head == null || k <= 1) {
return head;
}
Node dummy = new Node(0);
dummy.next = head;
Node groupPrev = dummy;
while (true) {
// Find kth node
Node kth = groupPrev;
for (int i = 0; i < k; i++) {
kth = kth.next;
if (kth == null) {
return dummy.next;
}
}
// Node after current group
Node groupNext = kth.next;
// Reverse current group
Node prev = groupNext;
Node current = groupPrev.next;
while (current != groupNext) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}
// Save old start
Node groupStart = groupPrev.next;
// Connect previous group to reversed group
groupPrev.next = kth;
// Move groupPrev to end of reversed group
groupPrev = groupStart;
}
}12. Understanding the Code
Let’s break down the important sections.
Find Kth Node
Node kth = groupPrev;
for (int i = 0; i < k; i++) {
kth = kth.next;
if (kth == null) {
return dummy.next;
}
}This ensures that a complete group of k nodes exists.
Save Next Group
Node groupNext = kth.next;We must save this before changing any pointers.
Otherwise, we could lose access to the remaining list.
Reverse
Node prev = groupNext;
Node current = groupPrev.next;
while (current != groupNext) {
Node next = current.next;
current.next = prev;
prev = current;
current = next;
}The important difference from normal linked list reversal is:
Node prev = groupNext;instead of:
Node prev = null;This automatically connects the reversed group to the next group.
Connect Previous Group
groupPrev.next = kth;kth is now the first node of the reversed group.
Move to Next Group
After reversal, the original first node becomes the last node.
So:
groupPrev = groupStart;moves us to the correct position for processing the next group.
13. Dry Run
Let’s use:
1 → 2 → 3 → 4 → 5 → 6with:
k = 3Initial State
dummy → 1 → 2 → 3 → 4 → 5 → 6
↑
groupPrevFind kth node:
kth = 3Save:
groupNext = 4So:
Group:
1 → 2 → 3
Next:
4 → 5 → 6Reverse Group 1
Before:
1 → 2 → 3 → 4After:
3 → 2 → 1 → 4Connect with dummy:
dummy → 3 → 2 → 1 → 4 → 5 → 6Now:
groupPrev = 1because 1 is the tail of the reversed group.
Process Group 2
Current list:
dummy → 3 → 2 → 1 → 4 → 5 → 6
↑
groupPrevFind kth node from groupPrev:
4 → 5 → 6Therefore:
kth = 6Save:
groupNext = nullReverse:
4 → 5 → 6becomes:
6 → 5 → 4Connect:
3 → 2 → 1 → 6 → 5 → 4Final:
3 → 2 → 1 → 6 → 5 → 414. Dry Run With Remaining Nodes
Consider:
1 → 2 → 3 → 4 → 5 → 6 → 7with:
k = 3Groups:
1 → 2 → 34 → 5 → 6Remaining:
7Reverse first group:
3 → 2 → 1Reverse second:
6 → 5 → 4There are not enough nodes for another group.
Get Pavan pithdiya’s stories in your inbox
Join Medium for free to get updates from this writer.
So 7 remains unchanged.
Final:
3 → 2 → 1 → 6 → 5 → 4 → 7This is exactly why we check:
if (kth == null) {
return dummy.next;
}15. Complexity
Suppose the linked list contains n nodes.
Time Complexity
O(n)Each node is processed a constant number of times.
Although we search for the kth node for each group, the total work remains linear.
Space Complexity
O(1)The algorithm uses only a few pointers.
Complexity Table
| Operation | Time | Space |
| -------------- | -------- | -------- |
| Find kth node | O(n) | O(1) |
| Reverse groups | O(n) | O(1) |
| Connect groups | O(n) | O(1) |
| **Total** | **O(n)** | **O(1)** |16. Edge Cases
Empty List
head = nullReturn:
nullk = 1
Nothing changes.
1 → 2 → 3Result:
1 → 2 → 3k Greater Than List Length
Input:
1 → 2 → 3and:
k = 5There is no complete group.
Result:
1 → 2 → 3List Length Equal to k
Input:
1 → 2 → 3k = 3Result:
3 → 2 → 1List Length Not Divisible by k
Input:
1 → 2 → 3 → 4 → 5k = 2Groups:
1 → 2
3 → 4Remaining:
5Result:
2 → 1 → 4 → 3 → 517. Common Mistakes
Mistake 1 — Reversing the Entire List
The problem asks for:
Groups of Knot the entire list.
For:
1 → 2 → 3 → 4 → 5 → 6and:
k = 3we need:
3 → 2 → 1 → 6 → 5 → 4not:
6 → 5 → 4 → 3 → 2 → 1Mistake 2 — Reversing an Incomplete Group
Input:
1 → 2 → 3 → 4 → 5with:
k = 3Correct:
3 → 2 → 1 → 4 → 5Do not reverse:
4 → 5because it contains fewer than k nodes.
Mistake 3 — Losing the Next Group
Before reversing, always save:
Node groupNext = kth.next;Otherwise, pointer changes can disconnect the remaining list.
Mistake 4 — Forgetting the Previous Group
After reversal, connect:
groupPrev.next = kth;Otherwise, the reversed group won’t be connected to the previous part.
Mistake 5 — Forgetting to Connect the Next Group
The old first node becomes the tail.
It must eventually point to:
groupNextUsing:
Node prev = groupNext;during reversal handles this connection elegantly.
Mistake 6 — Not Using a Dummy Node
Without a dummy node, the first group requires special handling because the head changes.
With:
Node dummy = new Node(0);
dummy.next = head;all groups can be handled using the same logic.
18. Pointer Map
The easiest way to understand this problem is to remember these four pointers:
PointerPurposegroupPrevNode before current groupgroupStartFirst node of current groupkthLast node of current groupgroupNextFirst node after current group
Before reversal:
groupPrev
↓
1 → 2 → 3 → 4
↑ ↑ ↑
groupStart kth groupNextAfter reversal:
groupPrev
↓
3 → 2 → 1 → 4
↑ ↑
kth groupStartThis pointer map makes the algorithm much easier to remember.
19. Relationship With Previous Problems
This problem combines several techniques from earlier articles.
Reverse Linked List
We already learned:
prev
curr
nextNow we apply the same technique to only part of the list.
Reverse Linked List II
In Part 12, we reversed a specific range:
left → rightHere, instead of positions, we repeatedly reverse fixed-size groups.
Swap Nodes in Pairs
When:
k = 2the problem becomes:
Swap Nodes in PairsFor example:
1 → 2 → 3 → 4becomes:
2 → 1 → 4 → 3So you can think of:
Reverse K-Groupas a generalized version of pair swapping.
20. Interview Follow-Up Questions
Q1. What happens if k = 1?
The list remains unchanged.
Q2. What happens if the remaining nodes are fewer than k?
They remain in their original order.
Q3. Can this be solved with O(1) extra space?
Yes.
The iterative pointer solution uses:
O(1)extra space.
Q4. Why use a dummy node?
It makes the first group behave exactly like every other group and avoids special handling for the head.
Q5. What happens when k = 2?
It becomes the Swap Nodes in Pairs problem.
Q6. Can the solution be implemented recursively?
Yes.
A recursive solution can:
- Check whether
knodes exist. - Reverse the first
knodes. - Recursively process the remaining list.
- Connect the two parts.
However, recursion requires additional call-stack space.
21. Recursive Idea
The recursive structure is:
reverse first K nodes
↓
reverse remaining groups
↓
connect both partsConceptually:
reverseKGroup(head, k)does:
Check K nodes
↓
Reverse K nodes
↓
head.next = reverseKGroup(nextGroup, k)
↓
Return new headA recursive implementation is elegant, but the iterative approach is usually better when the goal is O(1) auxiliary space.
22. Practice Problems
Problem 1 — Reverse in Groups of 2
Input:
1 → 2 → 3 → 4 → 5k = 2Expected:
2 → 1 → 4 → 3 → 5Problem 2 — Reverse in Groups of 3
Input:
1 → 2 → 3 → 4 → 5 → 6 → 7k = 3Expected:
3 → 2 → 1 → 6 → 5 → 4 → 7Problem 3 — Reverse Entire List
Input:
1 → 2 → 3 → 4k = 4Expected:
4 → 3 → 2 → 1Problem 4 — K Greater Than Length
Input:
1 → 2 → 3k = 5Expected:
1 → 2 → 3Key Takeaways
The most important ideas from this problem are:
Find Kth Node
↓
Save Next Group
↓
Reverse Current Group
↓
Connect Previous Group
↓
Connect Next Group
↓
RepeatRemember the four important pointers:
groupPrev
groupStart
kth
groupNextThe most important rule is:
Never reverse a group until you know that it contains K nodes.
For:
1 → 2 → 3 → 4 → 5 → 6and:
k = 3the result is:
3 → 2 → 1 → 6 → 5 → 4And for:
1 → 2 → 3 → 4 → 5with:
k = 3the result is:
3 → 2 → 1 → 4 → 5because the incomplete group remains unchanged.
The bigger lesson is:
Complex Linked List problems are usually built by combining simple pointer operations.
In this problem:
Dummy Node
+
Find Kth Node
+
Reverse
+
ReconnectOnce you understand these four pieces, Reverse Nodes in K-Group becomes much easier.
What’s Next?
The next advanced Linked List problem is one of the most interesting ones:
PART 14 — COPY LIST WITH RANDOM POINTER & LRU CACHE
We will go beyond the normal:
nextpointer and work with:
next + randomThen we’ll connect Linked Lists with a HashMap + Doubly Linked List to understand how an LRU Cache is implemented.
These are excellent problems for understanding how Linked Lists are used together with other data structures in real systems.








