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) 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. Java Program

import java.util.*;

public class PrepInsta
{

public static void main (String[]args) throws Exception
{

ll.display ();

ll.sortedInsert (40);

ll.display ();
}

}

{
private class Node
{
int data;
Node next;

// 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 + " ");
// Set temp to point to the next node
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 void sortedInsert (int 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)
{

}

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