# 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

1. 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.
2. 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.
3. 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.
4. Repeat steps 3 and 4 till the given linked list is empty.
5. 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
{

ll.display ();

ll.kReverse (2);
ll.display ();

}

}

{
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 tail;
private int size;

{
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 ()
{
while (temp != null)
{
System.out.print (temp.data + "  ");

temp = temp.next;
}
System.out.println ("END");
}

{
// 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)
{
}

// else set the head such that it now points to temp node
else
{

}

this.size++;
}

public int removeFirst () throws Exception
{
if (this.size () == 0)
{
throw new Exception ("Linked list is empty");
}

if (this.size () == 1)
{
}
else
{
}

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 ())
{
for (int i = 0; i < k && !this.isEmpty (); i++)
{

}

// If klist is null then curr becomes the klist
if (klist == null)
{
klist = curr;
}
else
{

klist.tail = curr.tail;
klist.size = klist.size + curr.size ();
}
}

// Make changes in head, tail and size of current linked list
```Output:Linked list before reversal11-->15-->27-->35-->51After reversal of given group 15-->11-->35-->27-->51