Java Program to reverse a linked list in groups of given size

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 .

Java Program to reverse a linked list in groups

Java program to Reverse a linked list in groups of given size

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.

Java Program to reverse a linked list in groups of given size

Algorithm

Step-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.

Step-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.

Step-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.

Step-4 Repeat steps 3 and 4 till the given linked list is empty.

Step-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 to find middle of a Linked List

Run
import java.util.*;

public class Main
{

  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);

      System.out.println ("Linked List before reversing");
      ll.display ();

      System.out.println (" ");

      ll.kReverse (2);

      System.out.println ("Linked List after reversing");

      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 reversing
11  15  27  35  51  END
 
Linked List after reversing
15  11  35  27  51  END

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java