Description
Reverse Nodes in k-Group
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list’s nodes, only nodes themselves may be changed.
Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:

Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Constraints:
- The number of nodes in the list is
n. 1 <= k <= n <= 50000 <= Node.val <= 1000
Follow-up: Can you solve the problem in O(1) extra memory space?
Approach
- Create a simple method to fetch the kth element from the head provided and create a dummy
- Loop with exit condition as the kth method is null
- Perform a simple reversal but keep in mind the value of previous and current just think of first iteration and what their values should be
- Lastly we have to maintain the previous group tail now and link kth element with the older one
Time:O(n) Space:O(1)- Simply iterating the linked list
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode getkth(ListNode list, int k) {
while (list != null && k > 0) {
list = list.next;
k--;
}
return list;
}
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dummy = new ListNode(0, head);
ListNode prevGroup = dummy;
while (true) {
ListNode kth = getkth(prevGroup, k);
if (kth == null)
break;
ListNode nextGroup = kth.next;
ListNode prev = kth.next;
ListNode curr = prevGroup.next;
while (curr != nextGroup) {
ListNode tmp = curr.next;
curr.next = prev;
prev = curr;
curr = tmp;
}
ListNode tmp = prevGroup.next;
prevGroup.next = kth;
prevGroup = tmp;
}
return dummy.next;
}
}Brute Force: Array / List Conversion
Extract all nodes into an array list, reverse groups of size in the array, and then re-link the pointers.
Intuition: “Dump to List Reverse sub-lists of size Re-link pointers.”
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
List<ListNode> nodes = new ArrayList<>();
ListNode curr = head;
while (curr != null) {
nodes.add(curr);
curr = curr.next;
}
int n = nodes.size();
for (int i = 0; i + k <= n; i += k) {
int left = i, right = i + k - 1;
while (left < right) {
ListNode temp = nodes.get(left);
nodes.set(left, nodes.get(right));
nodes.set(right, temp);
left++;
right--;
}
}
// Reconnect all nodes sequentially
for (int i = 0; i < n - 1; i++) {
nodes.get(i).next = nodes.get(i + 1);
}
if (n > 0) nodes.get(n - 1).next = null;
return nodes.isEmpty() ? null : nodes.get(0);
}
}
- Time Complexity: — One pass to collect nodes, one pass to swap pointers.
- Space Complexity: — Stores all nodes in an array list.
Most Optimized: In-Place Iterative Reversal
Iterate through the list and reverse each group of nodes in-place using a dummy node.
Mental Model (3 Steps Per Loop):
- Check: Find the -th node ahead. If less than nodes remain, stop.
- Reverse: Reverse the nodes standardly, setting the tail’s
nexttogroupNext. - Reconnect: Re-link the previous group’s tail to the new group head, and update
groupPrevto the group’s new tail.
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode dummy = new ListNode(0,head);
ListNode groupPrev = dummy;
while (true) {
ListNode kth = getKth(groupPrev, k);
if (kth == null) break;
ListNode groupNext = kth.next;
// Step 2: Reverse group nodes in-place
ListNode prev = groupNext;
ListNode curr = groupPrev.next;
while (curr != groupNext) {
ListNode tmp = curr.next;
curr.next = prev;
prev = curr;
curr = tmp;
}
// Step 3: Reconnect groupPrev to new head (kth)
ListNode newTail = groupPrev.next;
groupPrev.next = kth;
groupPrev = newTail;
}
return dummy.next;
}
private ListNode getKth(ListNode curr, int k) {
while (curr != null && k > 0) {
curr = curr.next;
k--;
}
return curr;
}
}
- Time Complexity: — Every node is visited twice (once to count , once to reverse).
- Space Complexity: — Uses constant extra memory space.