Java program for Insertion in a Sorted Linked List
February 8, 2023
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.
Algorithm
Step 1 :- Create a new node first with data equal to data to be inserted and next equal to null.
Step 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.
Step 2 :- Else, find the first node whose data is greater than the data to be inserted.
Step 3 :- Change the next of new node such that it points to next of node found in step 3.
Step 4 :- Change the next of node found in step 3 such that it points to new node.
Step 5 :- The data is inserted now and the linked list is still sorted.
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
import java.util.*;
class LinkedList
{
Node head;
// Node Class
class Node
{
int data;
Node next;
Node (int x) // parameterized constructor
{
data = x;
next = null;
}
}
private Node tail;
private int size;
// Linked list constructor
//
// 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 add a node in beginning of linked list
public Node insert (int data)
{
Node newNode = new Node (data);
newNode.next = head;
head = newNode;
return head;
}
public void display ()
{
Node node = head;
//as linked list will end when Node reaches Null
while (node != null)
{
System.out.print (node.data + " ");
node = node.next;
}
System.out.println ("\n");
}
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);
// 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;
}
}
}
public class Main
{
public static void main (String[]args) throws Exception
{
LinkedList ll = new LinkedList ();
ll.insert (97);
ll.insert (53);
ll.insert (21);
ll.insert (16);
System.out.println ("Linked List before Insertion");
ll.display ();
ll.sortedInsert (7);
System.out.println ("Linked List after Insertion");
ll.display ();
}
}
Output:
Linked List before Insertion
16 21 53 97
Linked List after Insertion
7 16 21 53 97
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment