Java program for Insertion in a Sorted Linked List

Insertion in a sorted linked list in java

What is insertion in a sorted linked list in java? It is a process of inserting data in between of the linked list, for that first we have to create a linked list which is already in a sorted form or we can say it is ascending/descending order, if it is not then we have to sort the linked list after that we will insert a node according to the data given to us in a node.

Example for Insertion in a sorted linked list in java. Suppose we have a linked list like ( 16–>21–>53–>97)  and we have to insert a node which have the data (40). After insertion linked list would look like or the output will be

Output -: (16–>21–>40–>53–>97)

Insertion of an element in a sorted linked list

Algorithm

  1. Create a new node first with data equal to data to be inserted and next equal to null.
  2.  If the linked list is empty or data to be inserted is less than the head node data then we need not do much. Simply change the next of new node such that it points to the head and update the head.
  3. Else, find the first node whose data is greater than the data to be inserted.
  4. Change the next of new node such that it points to next of node found in step 3.
  5. Change the next of node found in step 3 such that it points to new node.
  6. The data is inserted now and the linked list is still sorted.
Insertion-of-an-element-in-an-sorted-linked-list-in-Java

Java Program

import java.util.*;

public class PrepInsta
{

  public static void main (String[]args) throws Exception
  {
    LinkedList ll = new LinkedList ();
      ll.addFirst (16);
      ll.addFirst (21);
      ll.addFirst (53);
      ll.addFirst (97);
      
      ll.display ();

      ll.sortedInsert (40);

      ll.display ();
  }

}

class Insertion_in_LinkedList
{
  private class Node
  {
    int data;
    Node next;

// Node head
// 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;

// Linked list constructor
  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 + " ");
// Set temp to point to the next node
	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 void sortedInsert (int data)
  {
    this.sortedInsert (this.head, data);
  }

// Function to insert a node sch that the linked list remains sorted
  private void sortedInsert (Node node, int data)
  {
// Create a new node first
    Node newNode = new Node (data, null);

// If the linked list is empty or data to be inserted is less than the head node data then change the next of newNode such that it points to the head and update head
    if (node == null || data <= node.data)
      {
	newNode.next = this.head;
	this.head = newNode;

      }

    else
      {

// Move till we find the lastnode whose data is lesser than the data to be inserted
	while (node.next != null && node.next.data < data)
	  {
	    node = node.next;
	  }

// Now make changes to next pointers accordingly
	newNode.next = node.next;
	node.next = newNode;
      }

  }

}
Output
Linked list
16-->21-->53-->97
Element to be inserted
40
Linked list after insertion
16--> 21-->40-->53-->97