Java Program For Reversing A Linked List In Groups Of Given Size- Set 2 Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 5 Likes 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. Java // Java program to reverse a linked list // in groups of given size import java.util.*; class GfG { // Link list node static class Node { int data; Node next; } static Node head = null; /* Reverses the linked list in groups of size k and returns the pointer to the new head node. */ static Node Reverse(Node head, int k) { // Create a stack of Node* Stack<Node> mystack = new Stack<Node> (); Node current = head; 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.peek(); head = prev; mystack.pop(); } else { prev.next = mystack.peek(); 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 static void push( int new_data) { // Allocate node Node new_node = new Node(); // Put in the data new_node.data = new_data; // Link the old list of the // new node new_node.next = head; // Move the head to point to // the new node head = new_node; } // Function to print linked list static void printList(Node node) { while (node != null) { System.out.print(node.data + " "); node = node.next; } } // Driver code public static void main(String[] args) { // 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); System.out.println( "Given linked list "); printList(head); head = Reverse(head, 3); System.out.println(); System.out.println( "Reversed Linked list "); printList(head); } } // This code is contributed by Prerna Saini Output: Given Linked List 1 2 3 4 5 6 7 8 9 Reversed 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! Create Quiz Comment K kartik Follow 5 Improve K kartik Follow 5 Improve Article Tags : Java Linked Lists Microsoft Adobe VMWare Snapdeal Paytm Accolite MakeMyTrip +5 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like