











Java Program to reverse a linked list in groups of given size
Java Program to Reverse a linked list in groups of given size
Java Program to Reverse a linked list in groups of given size. For reversing a linked list in a group is not a big task to do. It also takes a same process of reversing as we have learned before in data structure but over here there is a minor difference in here we take two nodes as a head list . To reverse in a group we move two nodes or we can say we interchange them by each other.
Java Program to Reverse a linked list in groups of given size. As it is a user defined program we switch two nodes or three nodes that’s totally on us. If we switch or interchange two nodes in odd case the last element of a linked list will not get interchanged. It can happen even cases also if we switch 3 nodes at a time. We will learn more about it with the help of a java program.


Algorithm
- Take two linked list – curr and klist. At any time curr stores the k Nodes of present set in reverse order. Klist stores the reversed linked list in group.
- We traverse the linked list. For every k nodes we remove the node from the beginning of linked list and add them in the curr list ( Make use of addFirst function). Now curr list contains k elements in reverse order.
- Now we will put this set in klist and will go to the next set. There can be two cases: (a) If klist is empty then simply do klist= curr (b) If klist contains some reversed set already then change the next of tail of klist and add the current set present in the curr list to the klist.
- Repeat steps 3 and 4 till the given linked list is empty.
- Now make changes in head, tail and size of given linked list so that it becomes the klist and contains all k nodes set in reverse order.


Java Program
import java.util.*; public class PrepInsta { public static void main (String[]args) throws Exception { LinkedList ll = new LinkedList (); ll.addFirst (51); ll.addFirst (35); ll.addFirst (27); ll.addFirst (15); ll.addFirst (11); ll.display (); ll.kReverse (2); ll.display (); } } class LinkedList { private class Node { int data; Node next; // Node constructor // There are two fields in the node- data and address of next node public Node (int data, Node next) { this.data = data; this.next = next; } } private Node head; private Node tail; private int size; public LinkedList () { this.head = null; this.tail = null; this.size = 0; } // Function to find the size of linked list public int size () { return this.size; } // Function to check whether linked list is empty or not public boolean isEmpty () { return this.size () == 0; } // Function to traverse and print the linked list public void display () { Node temp = head; while (temp != null) { System.out.print (temp.data + " "); temp = temp.next; } System.out.println ("END"); } // Function to add a node in beginning of linked list public void addFirst (int item) { // Create a temp node which points to head Node temp = new Node (item, head); // If linked list is empty, temp is the head and tail node both if (this.size == 0) { this.head = this.tail = temp; } // else set the head such that it now points to temp node else { this.head = temp; } this.size++; } public int removeFirst () throws Exception { if (this.size () == 0) { throw new Exception ("Linked list is empty"); } int rv = this.head.data; if (this.size () == 1) { this.head = this.tail = null; } else { this.head = this.head.next; } this.size--; return rv; } // Function to reverse every k nodes public void kReverse (int k) throws Exception { LinkedList curr = null, klist = null; while (!this.isEmpty ()) { curr = new LinkedList (); for (int i = 0; i < k && !this.isEmpty (); i++) { // Remove from the linked list and add First in curr linked list curr.addFirst (this.removeFirst ()); } // If klist is null then curr becomes the klist if (klist == null) { klist = curr; } else { klist.tail.next = curr.head; klist.tail = curr.tail; klist.size = klist.size + curr.size (); } } // Make changes in head, tail and size of current linked list this.head = klist.head; this.tail = klist.tail; this.size = klist.size (); } }
Output:
Linked list before reversal
11-->15-->27-->35-->51
After reversal of given group
15-->11-->35-->27-->51
Login/Signup to comment