Open In App

Javascript Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders

Last Updated : 30 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
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 -> NULL
Output List: 10 -> 12 -> 30 -> 40 -> 53 -> 67 -> 89 -> NULL

Input List: 1 -> 4 -> 3 -> 2 -> 5 -> NULL
Output List: 1 -> 2 -> 3 -> 4 -> 5 -> NULL

Simple 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:  

  1. Separate two lists.
  2. Reverse the one with descending order
  3. Merge both lists.

Diagram: 

Below are the implementations of the above algorithm: 

Javascript




<script>
// Javascript program to sort a
// linked list that is alternatively
// sorted in increasing and decreasing order
 
// head of list
var 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 */
    var 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)
{
    var current = Dhead;
    var prev = null;
    var 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()
{
    var temp = head;
    while (temp != null)
    {
        document.write(temp.data + " ");
        temp = temp.next;
    }
    document.write();
}
 
// 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;
 
    var 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)
{
    var ascn = Ahead;
    var dscn = Dhead;
    var 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);
document.write("Given linked list<br/>");
printList();
 
sort();
 
document.write("<br/>Sorted linked list<br/>");
printList();
// This code contributed by aashish1995
</script>


Output: 

Given Linked List is
10 40 53 30 67 12 89
Sorted Linked List is
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!
 



Previous Article
Next Article

Similar Reads

C++ Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders
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 -> NULL Output List: 10 -> 12 -> 30 -> 40 -> 53 -> 67 -> 89 -> NULL Input List: 1 -> 4 -> 3 -> 2 -> 5 -> NULL Output List: 1 -> 2 -> 3 -> 4 -> 5 -> NULLRecommended: Please s
4 min read
Java Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders
Given a Linked List. The Linked List is in alternating ascending and descending orders. Sort the list efficiently. Example: Input List: 10 -&gt; 40 -&gt; 53 -&gt; 30 -&gt; 67 -&gt; 12 -&gt; 89 -&gt; NULL Output List: 10 -&gt; 12 -&gt; 30 -&gt; 40 -&gt; 53 -&gt; 67 -&gt; 89 -&gt; NULL Input List: 1 -&gt; 4 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; NULL Output L
4 min read
Python Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders
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 -> NULL Output List: 10 -> 12 -> 30 -> 40 -> 53 -> 67 -> 89 -> NULL Input List: 1 -> 4 -> 3 -> 2 -> 5 -> NULL Output List: 1 -> 2 -> 3 -> 4 -> 5 -> NULLRecommended: Please s
4 min read
Sort a linked list that is sorted alternating ascending and descending orders?
Given a Linked List. The Linked List is in alternating ascending and descending orders. Sort the list efficiently. Example: Input List: 10 -&gt; 40 -&gt; 53 -&gt; 30 -&gt; 67 -&gt; 12 -&gt; 89 -&gt; NULL Output List: 10 -&gt; 12 -&gt; 30 -&gt; 40 -&gt; 53 -&gt; 67 -&gt; 89 -&gt; NULL Input List: 1 -&gt; 4 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; NULL Output L
13 min read
Sort Matrix in alternating ascending and descending order rowwise
Given an N x N matrix, our task is to print the row of the matrix in ascending and descending order alternatively.Examples: Input: 5 7 3 4 9 5 8 2 6 3 8 1 5 8 9 3 Output: 3 4 5 7 9 8 5 2 1 3 6 8 9 8 5 3 Explanation: Here the first row is sorted in ascending order, second row sorted in descending order, third row sorted in ascending order and so on.
10 min read
Sorting Vector of Pairs by 1st element in ascending and 2nd element in descending
A pair is a container that stores two values mapped to each other, and a vector containing multiple numbers of such pairs is called a vector of pairs. While solving problems there come many instances where there is a need to sort the elements of vector on the basis of both the first and second elements of the pair. In that instance we have to pass
8 min read
Sorting rows of matrix in ascending order followed by columns in descending order
Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples : Input : a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 7 8 9 4 5 6 1 2 3 Input : a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output : 7 8 9 4 5 6 1 2 3 Approach: Traverse all rows one by one and sort rows in ascending o
10 min read
Sorting rows of matrix in descending order followed by columns in ascending order
Given a matrix of distinct elements. The task is to sort the rows of matrix in descending order followed by sorting the columns in ascending order. Examples: Input: a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output: 3 2 1 6 5 4 9 8 7 Input: a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output: 3 2 1 6 5 4 9 8 7 Approach: Traverse all rows one by o
9 min read
Sort an array of strings in ascending order with each string sorted in descending order
Given a array of strings S[] of size N (1 ? N ? 105), sort characters of each string in descending order and then print the array of strings in ascending order. Examples: Input: s[] = {"apple", "box", "cat"} Output: pplea tca xob Explanation: Sorting each string in descending order, S[] modifies to {"pplea", "xob", "tca"}. Sorting the array in asce
9 min read
Sort first k values in ascending order and remaining n-k values in descending order
Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order. Examples: Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4 Output: 2 4 5 6 9 8 3 1 -1 Input: arr[] = {5, 4, 6}, k = 2 Output: 4 5 6 Algorithm: Store the first k elements in an array and sort that in ascending
11 min read