Java program for Insertion in a Sorted Linked List

Java Program for Insertion in a Sorted Linked List

Here, you will learn how Insertion in a Sorted Linked List in Java works with clear explanations, simple algorithms, and complete Java code. This problem is commonly asked in coding interviews because it tests your understanding of ordered insertion, traversal, and pointer manipulation.

Java program for Insertion in a Sorted Linked List

Insertion in a Sorted Linked List in Java

What Does Inserting in a Sorted Linked List Mean?

In a sorted linked list, elements are already arranged in ascending order.
So when inserting a new value, you must place it at the correct position such that the list remains sorted.

Example:

Input list:

10 → 20 → 40 → 50

Insert 30

Output:

10 → 20 → 30 → 40 → 50

You are not allowed to sort after insertion. The new node must be placed correctly during the operation.

Learning Insertion in a Sorted Linked List helps with:

  • Maintaining ordered data
  • Real-time insertion without resorting
  • Understanding pointer-based insert operations
  • Strengthening DSA foundations
  • Handling edge cases like inserting at the beginning or end

This is also useful in priority queues, ordered sets, and incremental data structures.

Insertion in a Sorted Linked List:

Insertion in a sorted Linked List

Methods for Insertion in a Sorted Linked List in Java

Here are the 2 methods for Insertion in a Sorted Linked List in Java:

Method for Insertion in a Sorted Linked List in Java

Method 1: Iterative Traversal

Algorithm:

  1. Create a new node with the given value.

  2. If head == null, return new node.

  3. If new value is less than or equal to head.data, set new node as the new head.

  4. Initialize pointer current = head.

  5. Traverse while current.next != null and current.next.data < newValue.

  6. Insert the node between current and current.next.

  7. Return the head of the list.

Java Code:

Run
class SortedInsert {

    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    public static Node insertSorted(Node head, int value) {
        Node newNode = new Node(value);

        if (head == null || value <= head.data) {
            newNode.next = head;
            return newNode;
        }

        Node current = head;

        while (current.next != null && current.next.data < value) {
            current = current.next;
        }

        newNode.next = current.next;
        current.next = newNode;

        return head;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(40);

        System.out.println("Before insertion:");
        printList(head);

        head = insertSorted(head, 30);

        System.out.println("After insertion:");
        printList(head);
    }
}

Input:

10 20 40  
Insert: 30

Output:

10 20 30 40

Method for Insertion in a Sorted Linked List

Method 2: Recursive Sorted Insertion

Algorithm:

  1. If list is empty or value is smaller than head.data, return new node as new head.

  2. Recursively call insertion on head.next.

  3. Connect head.next to result of recursion.

  4. Return head.

Java Code:

Run
class SortedInsertRec {

    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    public static Node insertSortedRecursive(Node head, int value) {
        if (head == null || value <= head.data) {
            Node newNode = new Node(value);
            newNode.next = head;
            return newNode;
        }

        head.next = insertSortedRecursive(head.next, value);
        return head;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(5);
        head.next = new Node(15);
        head.next.next = new Node(25);

        System.out.println("Before insertion:");
        printList(head);

        head = insertSortedRecursive(head, 20);

        System.out.println("After insertion:");
        printList(head);
    }
}

Input:

5 15 25  
Insert: 20

Output:

5 15 20 25

Conclusion....

Insertion in a sorted singly linked list demonstrates how ordered data structures can be maintained using simple pointer adjustments.

MethodTime ComplexitySpace Complexity
Iterative TraversalO(n)O(1)
Recursive InsertionO(n)O(n)

Because the list is already sorted, the algorithm ensures the new value is inserted in the correct position while preserving the sorted order.

The iterative approach offers the most efficient solution with constant space, while the recursive method provides a cleaner but slightly more memory intensive alternative.

Understanding how Insertion in a Sorted Linked List in Java works helps build strong foundations for ordered data handling, priority structures, and efficient real time insert operations.

This concept also forms the basis for more advanced data structure problems such as merging sorted lists, implementing ordered queues, and maintaining dynamic sorted collections.

FAQ's related to Insertion in a Sorted Linked List in Java

Answer:

It means inserting a new value into the correct position in an already sorted list.

Answer:

Traverse until you find the correct position, then adjust pointers to place the new node.

Answer:

The new node becomes the new head of the list.

Answer:

Yes, but it uses extra stack space.

Answer:

The new node becomes the new head of the list.

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