04. Linked List Group Reverse
β GFG solution to the Linked List Group Reverse problem: reverse every k nodes in a linked list including remaining nodes using recursive and iterative approaches. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given the head of a Singly linked list. Your task is to reverse every k nodes in the linked list and return the head of the modified list.
Note: If the number of nodes is not a multiple of k, then the left-out nodes at the end should be considered as a group and must be reversed.
A group reversal means that within each group of k consecutive nodes, the order should be completely reversed while maintaining the overall structure of the linked list.
π Examples
Example 1
Input: k = 2,
Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Output: 2 -> 1 -> 4 -> 3 -> 6 -> 5
Explanation: Linked List is reversed in groups of size k = 2.
Groups: [1,2] -> [2,1], [3,4] -> [4,3], [5,6] -> [6,5]
Example 2
Input: k = 4,
Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Output: 4 -> 3 -> 2 -> 1 -> 6 -> 5
Explanation: Linked List is reversed in groups of size k = 4.
Groups: [1,2,3,4] -> [4,3,2,1], [5,6] -> [6,5] (remaining nodes also reversed)
Example 3
Input: k = 3,
Linked List: 1 -> 2 -> 3 -> 4 -> 5
Output: 3 -> 2 -> 1 -> 5 -> 4
Explanation: Groups: [1,2,3] -> [3,2,1], [4,5] -> [5,4]
π Constraints
$1 \le \text{size of linked list} \le 10^5$
$0 \le \text{node->data} \le 10^6$
$1 \le k \le \text{size of linked list}$
β
My Approach
The optimal approach uses Recursive Group Processing with Fast-Pointer Technique to efficiently handle both complete and partial groups:
Recursive Group Processing + Fast-Pointer
Count Nodes in Current Group:
Use a fast pointer to count exactly k nodes from current position.
If less than k nodes remain, treat remaining nodes as the final group.
Handle Partial Groups:
If current group has fewer than k nodes, reverse all remaining nodes.
This ensures the constraint that remaining nodes are also reversed.
Recursive Processing:
Recursively process the next group starting from the (k+1)th node.
This gives us the new head for the remaining processed list.
Reverse Current Group:
Reverse exactly k nodes (or remaining nodes if partial group).
Connect the reversed group to the already processed remaining list.
Return New Head:
Return the last node of the original group (which becomes the first after reversal).
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the total number of nodes in the linked list. Each node is visited exactly once during the traversal and reversal process.
Expected Auxiliary Space Complexity: O(n/k), where n is the total number of nodes and k is the group size. This space is used by the recursion stack, with maximum depth of n/k recursive calls.
π§βπ» Code (C++)
class Solution {
public:
Node* reverseKGroup(Node* head, int k) {
Node* cur = head; int cnt = 0;
while (cur && cnt < k) { cur = cur->next; cnt++; }
if (cnt < k) {
Node* prev = nullptr;
while (head) {
Node* nxt = head->next; head->next = prev; prev = head; head = nxt;
}
return prev;
}
cur = reverseKGroup(cur, k);
while (cnt--) {
Node* nxt = head->next; head->next = cur; cur = head; head = nxt;
}
return cur;
}
};
β Code (Java)
class Solution {
public Node reverseKGroup(Node head, int k) {
Node cur = head; int cnt = 0;
while (cur != null && cnt < k) { cur = cur.next; cnt++; }
if (cnt < k) {
Node prev = null;
while (head != null) {
Node nxt = head.next; head.next = prev; prev = head; head = nxt;
}
return prev;
}
cur = reverseKGroup(cur, k);
while (cnt-- > 0) {
Node nxt = head.next; head.next = cur; cur = head; head = nxt;
}
return cur;
}
}
π Code (Python)
class Solution:
def reverseKGroup(self, head, k):
cur, cnt = head, 0
while cur and cnt < k:
cur = cur.next; cnt += 1
if cnt < k:
prev = None
while head:
nxt = head.next; head.next = prev; prev = head; head = nxt
return prev
cur = self.reverseKGroup(cur, k)
while cnt:
nxt = head.next; head.next = cur; cur = head; head = nxt; cnt -= 1
return cur
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated