C++ Program For Reversing A Linked List In Groups Of Given Size - Set 2 Last Updated : 28 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->NULL and k = 5 Output: 5->4->3->2->1->8->7->6->NULL.Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have already discussed its solution in below post Reverse a Linked List in groups of given size | Set 1In this post, we have used a stack which will store the nodes of the given linked list. Firstly, push the k elements of the linked list in the stack. Now pop elements one by one and keep track of the previously popped node. Point the next pointer of prev node to top element of stack. Repeat this process, until NULL is reached.This algorithm uses O(k) extra space. C++ // C++ program to reverse a linked list // in groups of given size #include <bits/stdc++.h> using namespace std; // Link list node struct Node { int data; struct Node* next; }; /* Reverses the linked list in groups of size k and returns the pointer to the new head node. */ struct Node* Reverse(struct Node* head, int k) { // Create a stack of Node* stack<Node*> mystack; struct Node* current = head; struct Node* prev = NULL; while (current != NULL) { // Terminate the loop whichever // comes first either current == NULL // or count >= k int count = 0; while (current != NULL && count < k) { mystack.push(current); current = current->next; count++; } // Now pop the elements of stack // one by one while (mystack.size() > 0) { // If final list has not been // started yet. if (prev == NULL) { prev = mystack.top(); head = prev; mystack.pop(); } else { prev->next = mystack.top(); prev = prev->next; mystack.pop(); } } } // Next of last element will // point to NULL. prev->next = NULL; return head; } // UTILITY FUNCTIONS // Function to push a node void push(struct Node** head_ref, int new_data) { // Allocate node struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); // Put in the data new_node->data = new_data; // Link the old list of the // new node new_node->next = (*head_ref); // Move the head to point to // the new node (*head_ref) = new_node; } // Function to print linked list void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } // Driver code int main(void) { // Start with the empty list struct Node* head = NULL; // Created Linked list is // 1->2->3->4->5->6->7->8->9 push(&head, 9); push(&head, 8); push(&head, 7); push(&head, 6); push(&head, 5); push(&head, 4); push(&head, 3); push(&head, 2); push(&head, 1); printf("Given linked list "); printList(head); head = Reverse(head, 3); printf("Reversed Linked list "); printList(head); return 0; } Output: Given Linked List 1 2 3 4 5 6 7 8 9 Reversed list 3 2 1 6 5 4 9 8 7 Time complexity: O(n*k) where n is size of given linked list Auxiliary Space: O(1) Please refer complete article on Reverse a Linked List in groups of given size | Set 2 for more details! Comment More infoAdvertise with us Next Article Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2 K kartik Follow Improve Article Tags : C++ Linked Lists Microsoft Adobe VMWare Snapdeal Paytm Accolite MakeMyTrip +5 More Practice Tags : AccoliteAdobeMakeMyTripMicrosoftPaytmSnapdealVMWareCPP +4 More Similar Reads Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples:Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->NU 3 min read Javascript Program For Reversing A Linked List In Groups Of Given Size - Set 1 Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, 3 min read Reverse a Linked List in groups of given size Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed.Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 3 min read Reverse a Linked List in groups of given size using Stack Given a Singly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed.Examples: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 8 min read XOR Linked List - Reverse a Linked List in groups of given size Given a XOR linked list and an integer K, the task is to reverse every K nodes in the given XOR linked list. Examples: Input: XLL = 7< â > 6 < â > 8 < â > 11 < â > 3, K = 3 Output: 8 < â > 6 < â > 7 < â > 3 < â > 11 Explanation: Reversing first K(= 3) 13 min read Rearrange a Linked List in Zig-Zag fashion | Set-2 Given a linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c.. are consecutive data node of linked list. Note that it is not allowed to swap data. Examples: Input: 1->2->3->4 Output: 1->3->2->4 Input: 11-> 13 min read Like