Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2 Last Updated : 06 Sep, 2024 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 = 5Output: 5->4->3->2->1->8->7->6->NULL.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. JavaScript // Javascript program to reverse a linked list // in groups of given size { // Link list node class Node { constructor() { this.data = 0; this.next = null; } } let head = null; /* Reverses the linked list in groups of size k and returns the pointer to the new head node. */ function Reverse(head, k) { // Create a stack of Node* let mystack = []; let current = head; let prev = null; while (current != null) { // Terminate the loop whichever comes // first either current == NULL or // count >= k let 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.length > 0) { // If final list has not been // started yet. if (prev == null) { prev = mystack.pop(); head = prev; } else { prev.next = mystack.pop(); prev = prev.next; } } } // Next of last element will point // to NULL. prev.next = null; return head; } // UTILITY FUNCTIONS // Function to push a node function push(new_data) { // Allocate node let new_node = new Node(); // Put in the data new_node.data = new_data; // Link the old list off the // new node new_node.next = head; // Move the head to point to the // new node head = new_node; } // Function to print linked list function printList(node) { while (node != null) { console.log(node.data ); node = node.next; } } // Driver code // Start with the empty list // Node head = null; /* Created Linked list is 1->2->3-> 4->5->6->7->8->9 */ push(9); push(8); push(7); push(6); push(5); push(4); push(3); push(2); push(1); console.log("Given linked list "); printList(head); head = Reverse(head, 3); console.log("Reversed Linked list"); printList(head); // This code contributed by aashish1995 OutputGiven linked list 1 2 3 4 5 6 7 8 9 Reversed Linked list 3 2 1 6 5 4 9 8 7 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 Rearranging A Given Linked List In-Place K kartik Follow Improve Article Tags : JavaScript Linked Lists Microsoft Adobe VMWare Snapdeal Paytm Accolite MakeMyTrip +5 More Practice Tags : AccoliteAdobeMakeMyTripMicrosoftPaytmSnapdealVMWare +3 More Similar Reads 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 Javascript Program For Rearranging A Given Linked List In-Place Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 - 7 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 Javascript Program For Reversing Alternate K Nodes In A Singly Linked List Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm.Example: Inputs: 1->2->3->4->5->6->7->8->9->NULL and k = 3Output: 3->2->1->4->5->6->9 5 min read Javascript Program For Alternating Split Of A Given Singly Linked List- Set 1 Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and 3 min read Javascript Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List.See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, 5 min read Like