was trying out this LeetCode question
https://leetcode.com/problems/reverse-nodes-in-k-group/
The output supposed to be [3,2,1,4,5] but I keep on getting [3,2,1,4]. I believe there's problem with my current pointer but couldn't think stop it from referring to head.
I'm new to C++ by the way. Any assistance would be greatly appreciated.
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
// head = [1,2,3,4,5]
k = 3;
cout << "RECURSE" << endl;
ListNode* current = head;
ListNode* next = NULL;
ListNode* prev = NULL;
for(int i = 0; i < k; i++){
if(head == NULL){
return current;
}else{
next = head->next;
head->next = prev;
prev = head;
head = next;
}
}
current->next = reverseKGroup(head, k);
return prev;
}
};