25. Reverse Nodes in k-Group
Hard NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Linked List
- Time
- O(N)
- Space
- O(1)
Standard Solution (C++)
SummaryGiven a singly linked list and an integer k, reverse every consecutive group of k nodes; any remaining tail nodes that number fewer than k are left in their original order.
Approach
- Use a sentinel (dummy) node before the head to simplify edge cases and pointer manipulation.
- Before reversing each group, walk k steps forward to confirm at least k nodes remain; if not, stop.
- Reverse the k-node segment in-place with the standard three-pointer technique, then stitch the reversed segment back between the predecessor pointer and the rest of the list.
- Advance the predecessor pointer to the old group head (now the tail after reversal) and repeat until fewer than k nodes remain.
#include <cstddef>
using namespace std;
// Definition for singly-linked list (provided by the judge).
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode dummy(0, head);
ListNode* prev = &dummy; // node just before the current group
while (true) {
// Check whether at least k nodes remain.
ListNode* check = prev;
for (int i = 0; i < k; ++i) {
check = check->next;
if (!check) return dummy.next; // fewer than k left -- done
}
// groupHead will become the tail after reversal.
ListNode* groupHead = prev->next;
// Reverse k nodes starting at groupHead.
ListNode* cur = groupHead;
ListNode* back = nullptr;
for (int i = 0; i < k; ++i) {
ListNode* nxt = cur->next;
cur->next = back;
back = cur;
cur = nxt;
}
// back = new head of the reversed group
// cur = first node of the next group
// Reconnect the reversed segment.
prev->next = back; // predecessor -> new group head
groupHead->next = cur; // old head (now tail) -> next group
prev = groupHead; // advance predecessor for next iteration
}
}
};
Time O(n) Space O(1)
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if(k<=1)
return head;
ListNode dummyNode(0, head);
ListNode* group_start=&dummyNode;
ListNode* group_end=head;
int count=0;
while(group_end){
group_end=group_end->next;
++count;
if(count%k==0){
ListNode* group_start_next=group_start->next;
ListNode* new_group_start=reverseAGroup(group_start->next, k);
group_start->next=new_group_start;
group_start=group_start_next;
group_start->next=group_end;
}
}
return dummyNode.next;
}
ListNode* reverseAGroup(ListNode* head, int k) {
ListNode* prev=nullptr;
ListNode* cur=head;
ListNode* next_node=nullptr;
for(int i=0; i<k; ++i) {
next_node=cur->next;
cur->next=prev;
prev=cur;
cur=next_node;
}
return prev;
}
};
// Time Complexity: O(N)
// Space Complexity: O(1) Time O(N) Space O(1)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.