Javascript Program For Sorting Linked List Which Is Already Sorted On Absolute Values Last Updated : 03 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given a linked list that is sorted based on absolute values. Sort the list based on actual values.Examples: Input: 1 -> -10 Output: -10 -> 1Input: 1 -> -2 -> -3 -> 4 -> -5 Output: -5 -> -3 -> -2 -> 1 -> 4 Input: -5 -> -10 Output: -10 -> -5Input: 5 -> 10 Output: 5 -> 10Source : Amazon InterviewA simple solution is to traverse the linked list from beginning to end. For every visited node, check if it is out of order. If it is, remove it from its current position and insert it at the correct position. This is the implementation of insertion sort for linked list and the time complexity of this solution is O(n*n).A better solution is to sort the linked list using merge sort. Time complexity of this solution is O(n Log n).An efficient solution can work in O(n) time. An important observation is, all negative elements are present in reverse order. So we traverse the list, whenever we find an element that is out of order, we move it to the front of the linked list. Below is the implementation of the above idea.ever we find an element that is out of order, we move it to the front of the linked list. Below is the implementation of the above idea. JavaScript // Javascript program to sort a linked list, // already sorted by absolute values // head of list let head; // Linked list Node class Node { constructor(d) { this.data = d; this.next = null; } } // To sort a linked list by actual values. // The list is assumed to be sorted by // absolute values. function sortedList(head) { // Initialize previous and current // nodes let prev = head; let curr = head.next; // Traverse list while (curr != null) { // If curr is smaller than prev, then // it must be moved to head if (curr.data < prev.data) { // Detach curr from linked list prev.next = curr.next; // Move current node to beginning curr.next = head; head = curr; // Update current curr = prev; } // Nothing to do if current element // is at right place else prev = curr; // Move current curr = curr.next; } return head; } /* Inserts a new Node at front of the list. */ function push(new_data) { /* 1 & 2: Allocate the Node & Put in the data */ let new_node = new Node(new_data); // 3. Make next of new Node as head new_node.next = head; // 4. Move the head to point to // new Node head = new_node; } // Function to print linked list function printList(head) { let temp = head; while (temp != null) { console.log(temp.data); temp = temp.next; } } // Driver code /* Constructed Linked List is 1->2->3->4->5->6-> 7->8->8-> 9->null */ push(-5); push(5); push(4); push(3); push(-2); push(1); push(0); console.log("Original List :"); printList(head); head = sortedList(head); console.log("Sorted list :"); printList(head); // This code is contributed by aashish1995 Output: Original list :0 -> 1 -> -2 -> 3 -> 4 -> 5 -> -5Sorted list :-5 -> -2 -> 0 -> 1 -> 3 -> 4 -> 5Complexity Analysis:Time Complexity: O(N)Auxiliary Space: O(1)Please refer complete article on Sort linked list which is already sorted on absolute values for more details! Comment More infoAdvertise with us Next Article Javascript Program For Sorting Linked List Which Is Already Sorted On Absolute Values kartik Follow Improve Article Tags : Linked List JavaScript Web Technologies DSA Amazon Merge Sort Insertion Sort Linked-List-Sorting +4 More Practice Tags : AmazonLinked ListMerge Sort Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like