Insertion Sort for Singly Linked List

Last Updated : 22 Jun, 2026

Given the head of a singly linked list, sort the linked list in non-decreasing order using the Insertion Sort algorithm and return the head of the sorted list.

Examples:

Input:

blobid0_1781456820

Output: 2 -> 5 -> 8 -> 9
Explanation: After sorting the given linked list, the resultant list will be:

blobid5_1781457335


Input:

blobid3_1781456982

Output: 10 -> 20 -> 30 -> 40 -> 50 -> 60
Explanation: After sorting the given linked list, the resultant list will be:

blobid6_1781457385
Try It Yourself
redirect icon

The idea is to maintain a sorted portion of the linked list and process nodes one by one. If the current node is already in the correct position, extend the sorted portion. Otherwise, find its correct position in the sorted part and insert it there using a dummy node to simplify insertions.

Let us understand with example:
Input: 40 -> 20 -> 60 -> 10 -> 50 -> 30

  • Initially, the sorted part contains only 40, and the remaining list is 20 -> 60 -> 10 -> 50 -> 30.
  • Take node 20. Since it is smaller than 40, insert it before 40. The list becomes 20 -> 40 -> 60 -> 10 -> 50 -> 30.
  • Take node 60. Since it is greater than the last node in the sorted part (40), it is already in the correct position. The list remains 20 -> 40 -> 60 -> 10 -> 50 -> 30.
  • Take node 10. Find its correct position in the sorted part and insert it before 20. The list becomes 10 -> 20 -> 40 -> 60 -> 50 -> 30.
  • Take node 50. Find its correct position in the sorted part and insert it between 40 and 60. The list becomes 10 -> 20 -> 40 -> 50 -> 60 -> 30.
  • Take node 30. Find its correct position in the sorted part and insert it between 20 and 40. The list becomes 10 -> 20 -> 30 -> 40 -> 50 -> 60.
  • All nodes have now been processed, so the final sorted linked list is 10 -> 20 -> 30 -> 40 -> 50 -> 60.
C++
#include <climits>
#include <iostream>
using namespace std;

class Node
{
  public:
    int val;
    Node *next;

    Node(int x)
    {
        val = x;
        next = nullptr;
    }
};

// function for insertion sort on linked list
Node *insertionSort(Node *head)
{

    // Dummy node simplifies insertion at the beginning.
    Node *dummy = new Node(INT_MIN);
    dummy->next = head;

    // lastSorted stores the value of the last node
    // in the currently sorted portion.
    int lastSorted = INT_MIN;

    Node *prev = dummy;
    Node *curr = head;

    while (curr != nullptr)
    {

        // If current node is already in correct position,
        // extend the sorted portion.
        if (curr->val >= lastSorted)
        {
            lastSorted = curr->val;
            prev = curr;
            curr = curr->next;
            continue;
        }

        // Find insertion position in sorted part.
        Node *pos = dummy;
        while (curr->val >= pos->next->val)
        {
            pos = pos->next;
        }

        // Remove curr from current position.
        prev->next = curr->next;

        // Insert curr at correct position.
        curr->next = pos->next;
        pos->next = curr;

        curr = prev->next;
    }

    return dummy->next;
}

// Driver Code
int main()
{

    // Create linked list: 40 -> 20 -> 60 -> 10 -> 50 -> 30
    Node *head = new Node(40);
    head->next = new Node(20);
    head->next->next = new Node(60);
    head->next->next->next = new Node(10);
    head->next->next->next->next = new Node(50);
    head->next->next->next->next->next = new Node(30);

    head = insertionSort(head);

    // Print sorted linked list
    Node *temp = head;
    while (temp != nullptr)
    {
        cout << temp->val;
        if (temp->next != nullptr)
            cout << " -> ";
        temp = temp->next;
    }
    cout << endl;

    return 0;
}
Java
import java.util.*;

class Node {
    int val;
    Node next;

    Node(int x)
    {
        val = x;
        next = null;
    }
}

public class GFG {
    // function for insertion sort on linked list
    static Node insertionSort(Node head)
    {
        // Dummy node simplifies insertion at the beginning.
        Node dummy = new Node(Integer.MIN_VALUE);
        dummy.next = head;

        // lastSorted stores the value of the last node
        // in the currently sorted portion.
        int lastSorted = Integer.MIN_VALUE;

        Node prev = dummy;
        Node curr = head;

        while (curr != null) {
            // If current node is already in correct
            // position, extend the sorted portion.
            if (curr.val >= lastSorted) {
                lastSorted = curr.val;
                prev = curr;
                curr = curr.next;
                continue;
            }

            // Find insertion position in sorted part.
            Node pos = dummy;
            while (curr.val >= pos.next.val) {
                pos = pos.next;
            }

            // Remove curr from current position.
            prev.next = curr.next;

            // Insert curr at correct position.
            curr.next = pos.next;
            pos.next = curr;

            curr = prev.next;
        }

        return dummy.next;
    }

    public static void main(String[] args)
    {
        // Create linked list: 40 -> 20 -> 60 -> 10 -> 50 ->
        // 30
        Node head = new Node(40);
        head.next = new Node(20);
        head.next.next = new Node(60);
        head.next.next.next = new Node(10);
        head.next.next.next.next = new Node(50);
        head.next.next.next.next.next = new Node(30);

        head = insertionSort(head);

        // Print sorted linked list
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.val);
            if (temp.next != null)
                System.out.print(" -> ");
            temp = temp.next;
        }
        System.out.println();
    }
}
Python
class Node:
    def __init__(self, x):
        self.val = x
        self.next = None

# function for insertion sort on linked list


def insertionSort(head):

    # Dummy node simplifies insertion at the beginning.
    dummy = Node(float('-inf'))
    dummy.next = head

    # lastSorted stores the value of the last node
    # in the currently sorted portion.
    lastSorted = float('-inf')

    prev = dummy
    curr = head

    while curr is not None:

        # If current node is already in correct position,
        # extend the sorted portion.
        if curr.val >= lastSorted:
            lastSorted = curr.val
            prev = curr
            curr = curr.next
            continue

        # Find insertion position in sorted part.
        pos = dummy
        while curr.val >= pos.next.val:
            pos = pos.next

        # Remove curr from current position.
        prev.next = curr.next

        # Insert curr at correct position.
        curr.next = pos.next
        pos.next = curr

        curr = prev.next

    return dummy.next


# Driver Code
if __name__ == '__main__':

    # Create linked list: 40 -> 20 -> 60 -> 10 -> 50 -> 30
    head = Node(40)
    head.next = Node(20)
    head.next.next = Node(60)
    head.next.next.next = Node(10)
    head.next.next.next.next = Node(50)
    head.next.next.next.next.next = Node(30)

    head = insertionSort(head)

    # Print sorted linked list
    temp = head
    while temp is not None:
        print(temp.val, end='')
        if temp.next is not None:
            print(' -> ', end='')
        temp = temp.next
    print()
C#
using System;

public class Node {
    public int val;
    public Node next;

    public Node(int x)
    {
        val = x;
        next = null;
    }
}

public class GFG {

    // function for insertion sort on linked list
    public static Node insertionSort(Node head)
    {

        // Dummy node simplifies insertion at the beginning.
        Node dummy = new Node(int.MinValue);
        dummy.next = head;

        // lastSorted stores the value of the last node
        // in the currently sorted portion.
        int lastSorted = int.MinValue;

        Node prev = dummy;
        Node curr = head;

        while (curr != null) {

            // If current node is already in correct
            // position, extend the sorted portion.
            if (curr.val >= lastSorted) {
                lastSorted = curr.val;
                prev = curr;
                curr = curr.next;
                continue;
            }

            // Find insertion position in sorted part.
            Node pos = dummy;
            while (curr.val >= pos.next.val) {
                pos = pos.next;
            }

            // Remove curr from current position.
            prev.next = curr.next;

            // Insert curr at correct position.
            curr.next = pos.next;
            pos.next = curr;

            curr = prev.next;
        }

        return dummy.next;
    }

    public static void Main(string[] args)
    {
        // Create linked list: 40 -> 20 -> 60 -> 10 -> 50 ->
        // 30
        Node head = new Node(40);
        head.next = new Node(20);
        head.next.next = new Node(60);
        head.next.next.next = new Node(10);
        head.next.next.next.next = new Node(50);
        head.next.next.next.next.next = new Node(30);

        head = insertionSort(head);

        // Print sorted linked list
        Node temp = head;
        while (temp != null) {
            Console.Write(temp.val);
            if (temp.next != null)
                Console.Write(" -> ");
            temp = temp.next;
        }
        Console.WriteLine();
    }
}
JavaScript
class Node {
    constructor(val)
    {
        this.val = val;
        this.next = null;
    }
}

// function for insertion sort on linked list
function insertionSort(head)
{

    // Dummy node simplifies insertion at the beginning.
    const dummy = new Node(Number.MIN_SAFE_INTEGER);
    dummy.next = head;

    // lastSorted stores the value of the last node
    // in the currently sorted portion.
    let lastSorted = Number.MIN_SAFE_INTEGER;

    let prev = dummy;
    let curr = head;

    while (curr != null) {

        // If current node is already in correct position,
        // extend the sorted portion.
        if (curr.val >= lastSorted) {
            lastSorted = curr.val;
            prev = curr;
            curr = curr.next;
            continue;
        }

        // Find insertion position in sorted part.
        let pos = dummy;
        while (curr.val >= pos.next.val) {
            pos = pos.next;
        }

        // Remove curr from current position.
        prev.next = curr.next;

        // Insert curr at correct position.
        curr.next = pos.next;
        pos.next = curr;

        curr = prev.next;
    }

    return dummy.next;
}

// Driver Code
let head = new Node(40);
head.next = new Node(20);
head.next.next = new Node(60);
head.next.next.next = new Node(10);
head.next.next.next.next = new Node(50);
head.next.next.next.next.next = new Node(30);

head = insertionSort(head);

// Print sorted linked list
let temp = head;
while (temp != null) {
    process.stdout.write(temp.val.toString());
    if (temp.next != null)
        process.stdout.write(" -> ");
    temp = temp.next;
}
console.log();

Output
10 -> 20 -> 30 -> 40 -> 50 -> 60
Comment