Javascript Program For QuickSort On Singly Linked List Last Updated : 09 Sep, 2024 Comments Improve Suggest changes Like Article Like Report QuickSort on Doubly Linked List is discussed here. QuickSort on Singly linked list was given as an exercise. The important things about implementation are, it changes pointers rather swapping data and time complexity is same as the implementation for Doubly Linked List.In partition(), we consider last element as pivot. We traverse through the current list and if a node has value greater than pivot, we move it after tail. If the node has smaller value, we keep it at its current position. In QuickSortRecur(), we first call partition() which places pivot at correct position and returns pivot. After pivot is placed at correct position, we find tail node of left side (list before pivot) and recur for left list. Finally, we recur for right list. JavaScript // Javascript program for Quick Sort on // Singly LinKed List // Sort a linked list using quick sort class Node { constructor(val) { this.data = val; this.next = null; } } let head; function addNode(data) { if (head == null) { head = new Node(data); return; } let curr = head; while (curr.next != null) curr = curr.next; let newNode = new Node(data); curr.next = newNode; } function printList(n) { while (n != null) { console.log(n.data); n = n.next; } } // Takes first and last node, // but do not break any links in // the whole linked list function partitionLast(start, end) { if (start == end || start == null || end == null) return start; let pivot_prev = start; let curr = start; let pivot = end.data; // Iterate till one before the end, // no need to iterate till the end // because end is pivot while (start != end) { if (start.data < pivot) { // Keep tracks of last modified // item pivot_prev = curr; let temp = curr.data; curr.data = start.data; start.data = temp; curr = curr.next; } start = start.next; } // Swap the position of curr i.e. // next suitable index and pivot let temp = curr.data; curr.data = pivot; end.data = temp; // Return one previous to current // because current is now pointing // to pivot return pivot_prev; } function sort(start, end) { if (start == null || start == end || start == end.next) return; // Split list and partition recurse let pivot_prev = partitionLast(start, end); sort(start, pivot_prev); // If pivot is picked and moved to the start, // that means start and pivot is same // so pick from next of pivot if (pivot_prev != null && pivot_prev == start) sort(pivot_prev.next, end); // If pivot is in between of the list, // start from next of pivot, // since we have pivot_prev, so we move two nodes else if (pivot_prev != null && pivot_prev.next != null) sort(pivot_prev.next.next, end); } // Driver Code addNode(30); addNode(3); addNode(4); addNode(20); addNode(5); let n = head; while (n.next != null) n = n.next; console.log("Linked List before sorting"); printList(head); sort(head, n); console.log("Linked List after sorting"); printList(head); // This code is contributed by umadevi9616 OutputLinked List before sorting 30 3 4 20 5 Linked List after sorting 3 4 5 20 30 Time Complexity: O(N * log N), It takes O(N2) time in the worst case and O(N log N) in the average or best case.Please refer complete article on QuickSort on Singly Linked List for more details! Comment More infoAdvertise with us kartik Follow Improve Article Tags : Linked List Sorting JavaScript Web Technologies DSA Quick Sort Linked-List-Sorting +3 More Practice Tags : Linked ListSorting Similar Reads JavaScript Linked List Programs JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesnât store the elements contiguously like an array. S. NoArticles1JavaScript Program 5 min read Implementation of LinkedList in Javascript In this article, we will be implementing the LinkedList data structure in Javascript.A linked list is a linear data structure where elements are stored in nodes, each containing a value and a reference (or pointer) to the next node. It allows for efficient insertion and deletion operations.Each node 5 min read Javascript Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise.bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functio 3 min read Javascript Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node 7 min read Javascript Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node.Examples: Input : list: 1->2->4->5 x = 3Output : 1->2->3- 4 min read Javascript Program For Writing A Function To Delete A Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. This article focuses on writing a function to delete a linked list.Implementation: JavaScript// Javascript program to delete // a li 1 min read Javascript Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position.Example: Input: position = 1, Linked List = 8->2->3->1->7Output: Linked List = 8->3->1->7Input: position = 0, Linked List = 8->2->3->1->7Output: Linked List = 2->3->1- 3 min read Javascript Program For Finding Length Of A Linked List Write a function to count the number of nodes in a given singly linked list.For example, the function should return 5 for linked list 1->3->1->2->1.Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head.3) Do following while current is not NULL a) curre 3 min read Javascript Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal 5 min read Javascript Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples:Input: 1 2 3 4 5 Output: 3 1 2 4 5Input: 1 2 3 4 5 6Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves on 3 min read Like