21. Reverse a Linked List in groups of given size
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
struct node *reverse(struct node *head, int k) {
if (head == NULL)
return head;
struct node *prev = NULL, *curr = head, *temp = head;
int it = k;
while (it-- && curr) {
temp = temp->next;
curr->next = prev;
prev = curr;
curr = temp;
}
head->next = reverse(curr, k);
return prev;
}
};Contribution and Support
Last updated