Java Program For Reversing A Linked List In Groups Of Given Size - Set 1 Last Updated : 16 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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, 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. Algorithm: reverse(head, k) Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev. See this post for reversing a linked list.head->next = reverse(next, k) ( Recursively call for rest of the list and link the two sub-lists )Return prev ( prev becomes the new head of the list (see the diagrams of an iterative method of this post ) Below is image shows how the reverse function works: Below is the implementation of the above approach: Java // Java program to reverse a // linked list in groups of // given size class LinkedList { // Head of list Node head; // Linked list Node class Node { int data; Node next; Node(int d) { data = d; next = null; } } Node reverse(Node head, int k) { if(head == null) return null; Node current = head; Node next = null; Node prev = null; int count = 0; // Reverse first k nodes of // linked list while (count < k && current != null) { next = current.next; current.next = prev; prev = current; current = next; count++; } /* next is now a pointer to (k+1)th node Recursively call for the list starting from current. And make rest of the list as next of first node */ if (next != null) head.next = reverse(next, k); // prev is now head of the input list return prev; } // Utility functions // Inserts a new Node at front // of the list. public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); // 3. Make next of new Node // as head new_node.next = head; // 4. Move the head to point to // new Node head = new_node; } // Function to print linked list void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } // Driver code public static void main(String args[]) { LinkedList llist = new LinkedList(); // Create Linked List is // 1->2->3->4->5->6-> // 7->8->8->9->null llist.push(9); llist.push(8); llist.push(7); llist.push(6); llist.push(5); llist.push(4); llist.push(3); llist.push(2); llist.push(1); System.out.println("Given Linked List"); llist.printList(); llist.head = llist.reverse(llist.head, 3); System.out.println("Reversed list"); llist.printList(); } } // This code is contributed by Rajat Mishra Output: Given Linked List 1 2 3 4 5 6 7 8 9 Reversed list 3 2 1 6 5 4 9 8 7 Complexity Analysis: Time Complexity: O(n). Traversal of list is done only once and it has 'n' elements.Auxiliary Space: O(n/k). For each Linked List of size n, n/k or (n/k)+1 calls will be made during the recursion. Please refer complete article on Reverse a Linked List in groups of given size | Set 1 for more details! Comment More infoAdvertise with us Next Article Java Program For Reversing A Linked List In Groups Of Given Size - Set 1 kartik Follow Improve Article Tags : Linked List Java Java Programs DSA Linked Lists Microsoft Amazon Adobe VMWare Snapdeal Paytm Accolite Hike SAP Labs MakeMyTrip Amazon-Question Yatra.com-Question Snapdeal-Question Reverse +15 More Practice Tags : AccoliteAdobeAmazonHikeMakeMyTripMicrosoftPaytmSAP LabsSnapdealVMWareJavaLinked ListReverse +9 More Similar Reads Java 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->N 3 min read Java 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 - 8 min read Java 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 = 3 Output: 3->2->1->4->5->6- 6 min read Java 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 2 min read Java Program for Reverse a linked list Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In 3 min read Java 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 Java Program For Segregating Even And Odd Nodes In A Linked List Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NUL 6 min read Java Program For Pairwise Swapping Elements Of A Given Linked List Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp 3 min read Java Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15- 6 min read Java Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal 6 min read Like