Javascript Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Given a Linked List. The Linked List is in alternating ascending and descending orders. Sort the list efficiently. Example: Input List: 10 -> 40 -> 53 -> 30 -> 67 -> 12 -> 89 -> NULLOutput List: 10 -> 12 -> 30 -> 40 -> 53 -> 67 -> 89 -> NULLInput List: 1 -> 4 -> 3 -> 2 -> 5 -> NULLOutput List: 1 -> 2 -> 3 -> 4 -> 5 -> NULLSimple Solution: Approach: The basic idea is to apply to merge sort on the linked list. The implementation is discussed in this article: Merge Sort for linked List.Complexity Analysis: Time Complexity: The merge sort of linked list takes O(n log n) time. In the merge sort tree, the height is log n. Sorting each level will take O(n) time. So time complexity is O(n log n).Auxiliary Space: O(n log n), In the merge sort tree the height is log n. Storing each level will take O(n) space. So space complexity is O(n log n).Efficient Solution: Approach: Separate two lists.Reverse the one with descending orderMerge both lists.Diagram: Below are the implementations of the above algorithm: JavaScript // Javascript program to sort a // linked list that is alternatively // sorted in increasing and decreasing order // head of list let head; // Linked list Node class Node { constructor(val) { this.data = val; this.next = null; } } function newNode(key) { return new Node(key); } /* This is the main function that sorts the linked list. */ function sort() { /* Create 2 dummy nodes and initialise as heads of linked lists */ let Ahead = new Node(0), Dhead = new Node(0); // Split the list into lists splitList(Ahead, Dhead); Ahead = Ahead.next; Dhead = Dhead.next; // Reverse the descending list Dhead = reverseList(Dhead); // Merge the 2 linked lists head = mergeList(Ahead, Dhead); } // Function to reverse the linked list function reverseList(Dhead) { let current = Dhead; let prev = null; let next; while (current != null) { next = current.next; current.next = prev; prev = current; current = next; } Dhead = prev; return Dhead; } // Function to print linked list function printList() { let temp = head; while (temp != null) { console.log(temp.data + " "); temp = temp.next; } console.log(); } // A utility function to merge // two sorted linked lists function mergeList(head1, head2) { // Base cases if (head1 == null) return head2; if (head2 == null) return head1; let temp = null; if (head1.data < head2.data) { temp = head1; head1.next = mergeList(head1.next, head2); } else { temp = head2; head2.next = mergeList(head1, head2.next); } return temp; } // This function alternatively splits // a linked list with head as head into two: // For example, 10->20->30->15->40->7 is // splitted into 10->30->40 and 20->15->7 // "Ahead" is reference to head of ascending // linked list // "Dhead" is reference to head of descending // linked list function splitList(Ahead, Dhead) { let ascn = Ahead; let dscn = Dhead; let curr = head; // Link alternate nodes while (curr != null) { // Link alternate nodes in // ascending order ascn.next = curr; ascn = ascn.next; curr = curr.next; if (curr != null) { dscn.next = curr; dscn = dscn.next; curr = curr.next; } } ascn.next = null; dscn.next = null; } // Driver code head = newNode(10); head.next = newNode(40); head.next.next = newNode(53); head.next.next.next = newNode(30); head.next.next.next.next = newNode(67); head.next.next.next.next.next = newNode(12); head.next.next.next.next.next.next = newNode(89); console.log("Given linked list"); printList(); sort(); console.log("Sorted linked list"); printList(); // This code contributed by aashish1995 OutputGiven linked list 10 40 53 30 67 12 89 Sorted linked list 10 12 30 40 53 67 89 Complexity Analysis: Time Complexity: O(n). One traversal is needed to separate the list and reverse them. The merging of sorted lists takes O(n) time.Auxiliary Space: O(1). No extra space is required.Please refer complete article on Sort a linked list that is sorted alternating ascending and descending orders? for more details! Comment More infoAdvertise with us Next Article Javascript Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders kartik Follow Improve Article Tags : Linked List Sorting JavaScript Web Technologies DSA Amazon Merge Sort Linked-List-Sorting +4 More Practice Tags : AmazonLinked ListMerge SortSorting 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