Javascript Program For Insertion Sort In A Singly Linked List Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert current node in sorted way in sorted or result list. 3) Change head of given linked list to head of sorted (or result) list.The main step is (2.a) which has been covered in the post Sorted Insert for Singly Linked List Below is the implementation of the above algorithm: JavaScript // Javascript program to sort link list // using insertion sort let head = null; let sorted = null; class node { constructor(val) { this.val = val; this.next = null; } } function push(val) { // Allocate node let newnode = new node(val); // Link the old list off the // new node newnode.next = head; // Move the head to point to // the new node head = newnode; } // Function to sort a singly linked list // using insertion sort function insertionSort(headref) { // Initialize sorted linked list let sorted = null; let current = headref; // Traverse the given linked list // and insert every node to sorted while (current != null) { // Store next for next iteration let next = current.next; // Insert current in sorted // linked list sortedInsert(current); // Update current current = next; } // Update head_ref to point to // sorted linked list head = sorted; } /* Function to insert a new_node in a Linked List. Note that this function expects a pointer to head_ref as this can modify the head of the input linked list (similar to push()) */ function sortedInsert(newnode) { // Special case for the head end if (sorted == null || sorted.val >= newnode.val) { newnode.next = sorted; sorted = newnode; } else { let current = sorted; /* Locate the node before the point of insertion */ while (current.next != null && current.next.val < newnode.val) { current = current.next; } newnode.next = current.next; current.next = newnode; } } // Function to print linked list function printlist(head) { while (head != null) { console.log(head.val); head = head.next; } } // Driver code push(5); push(20); push(4); push(3); push(30); console.log("Linked List before Sorting.."); printlist(head); insertionSort(head); console.log("LinkedList After sorting"); printlist(sorted); // This code is contributed by aashish1995 OutputLinked List before Sorting.. 30 3 4 20 5 LinkedList After sorting 3 4 5 20 30 Complexity Analysis:Time Complexity: O(n2), in the worst case, we might have to traverse all nodes of the sorted list for inserting a node, and there are “n” such nodes.Space Complexity: O(1), no extra space is required depending on the size of the input, thus it is constant.Please refer complete article on Insertion Sort for Singly Linked List for more details! Comment More info K kartik Follow Improve Article Tags : JavaScript Insertion Sort Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like