C++ Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders Last Updated : 28 Dec, 2021 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 -> 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 solve it on "PRACTICE" first, before moving on to the solution. 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: Separate two lists.Reverse the one with descending orderMerge both lists. Diagram: Below are the implementations of the above algorithm: C++ // C++ program to sort a linked // list that is alternatively sorted // in increasing and decreasing order #include <bits/stdc++.h> using namespace std; // Linked list node struct Node { int data; struct Node* next; }; Node* mergelist(Node* head1, Node* head2); void splitList(Node* head, Node** Ahead, Node** Dhead); void reverselist(Node*& head); // This is the main function that // sorts the linked list void sort(Node** head) { // Split the list into lists Node *Ahead, *Dhead; splitList(*head, &Ahead, &Dhead); // Reverse the descending linked list reverselist(Dhead); // Merge the two linked lists *head = mergelist(Ahead, Dhead); } // A utility function to create a // new node Node* newNode(int key) { Node* temp = new Node; temp->data = key; temp->next = NULL; return temp; } // A utility function to reverse a // linked list void reverselist(Node*& head) { Node *prev = NULL, *curr = head, *next; while (curr) { next = curr->next; curr->next = prev; prev = curr; curr = next; } head = prev; } // A utility function to print // a linked list void printlist(Node* head) { while (head != NULL) { cout << head->data << " "; head = head->next; } cout << endl; } // A utility function to merge // two sorted linked lists Node* mergelist(Node* head1, Node* head2) { // Base cases if (!head1) return head2; if (!head2) return head1; Node* 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 void splitList(Node* head, Node** Ahead, Node** Dhead) { // Create two dummy nodes to // initialize heads of two // linked list *Ahead = newNode(0); *Dhead = newNode(0); Node* ascn = *Ahead; Node* dscn = *Dhead; Node* curr = head; // Link alternate nodes while (curr) { // Link alternate nodes of ascending // linked list ascn->next = curr; ascn = ascn->next; curr = curr->next; // Link alternate nodes of descending // linked list if (curr) { dscn->next = curr; dscn = dscn->next; curr = curr->next; } } ascn->next = NULL; dscn->next = NULL; *Ahead = (*Ahead)->next; *Dhead = (*Dhead)->next; } // Driver code int main() { Node* 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); cout << "Given Linked List is " << endl; printlist(head); sort(&head); cout << "Sorted Linked List is " << endl; printlist(head); return 0; } 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! Comment More infoAdvertise with us Next Article C++ Program For Sorting A Linked List That Is Sorted Alternating Ascending And Descending Orders kartik Follow Improve Article Tags : Linked List Sorting C++ Programs DSA Amazon Merge Sort Linked-List-Sorting +3 More Practice Tags : AmazonLinked ListMerge SortSorting Similar Reads C++ Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where 8 min read C++ Program For Sorting Linked List Which Is Already Sorted On Absolute Values Given a linked list that is sorted based on absolute values. Sort the list based on actual values.Examples: Input: 1 -> -10 Output: -10 -> 1 Input: 1 -> -2 -> -3 -> 4 -> -5 Output: -5 -> -3 -> -2 -> 1 -> 4 Input: -5 -> -10 Output: -10 -> -5 Input: 5 -> 10 Outpu 3 min read C++ Program For Segregating Even And Odd Nodes In A Linked List Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers the same. Examples: Input: 17->15->8->12->10->5->4->1->7->6- 7 min read C++ Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 3 min read C++ Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 2->1->2->1->1->2->0->1->0 Output: 0->0->1->1->1->1->2->2->2 The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2. Input: 2->1->0 Output: 0->1->2 The sorted Array is 0, 1, 2Recommended: Please solve it on "PRACTICE" first, before moving on to th 3 min read C++ Program For Comparing Two Strings Represented As Linked Lists Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicograph 2 min read C++ Program For Sorting A Linked List Of 0s, 1s And 2s Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULL Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL So 3 min read C++ Program For Rearranging A Given List Such That It Consists Of Alternating Minimum Maximum Elements Given a list of integers, rearrange the list such that it consists of alternating minimum-maximum elements using only list operations. The first element of the list should be minimum and the second element should be the maximum of all elements present in the list. Similarly, the third element will b 2 min read C++ Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15- 5 min read C++ Program For Partitioning A Linked List Around A Given Value And Keeping The Original Order Given a linked list and a value x, partition it such that all nodes less than x come first, then all nodes with a value equal to x, and finally nodes with a value greater than or equal to x. The original relative order of the nodes in each of the three partitions should be preserved. The partition m 4 min read Like