C++ Program For Sorting A Linked List Of 0s, 1s And 2s Last Updated : 10 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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 Source: Microsoft Interview | Set 1 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Following steps can be used to sort the given linked list. Traverse the list and count the number of 0s, 1s, and 2s. Let the counts be n1, n2, and n3 respectively.Traverse the list again, fill the first n1 nodes with 0, then n2 nodes with 1, and finally n3 nodes with 2. Below image is a dry run of the above approach: Below is the implementation of the above approach: C++ // C++ Program to sort a linked list // 0s, 1s or 2s #include <bits/stdc++.h> using namespace std; // Link list node class Node { public: int data; Node* next; }; // Function to sort a linked list // of 0s, 1s and 2s void sortList(Node *head) { // Initialize count of '0', '1' // and '2' as 0 int count[3] = {0, 0, 0}; Node *ptr = head; /* Count total number of '0', '1' and '2' count[0] will store total number of '0's count[1] will store total number of '1's count[2] will store total number of '2's */ while (ptr != NULL) { count[ptr->data] += 1; ptr = ptr->next; } int i = 0; ptr = head; /* Let say count[0] = n1, count[1] = n2 and count[2] = n3. Now start traversing list from head node, 1) fill the list with 0, till n1 > 0 2) fill the list with 1, till n2 > 0 3) fill the list with 2, till n3 > 0 */ while (ptr != NULL) { if (count[i] == 0) ++i; else { ptr->data = i; --count[i]; ptr = ptr->next; } } } // Function to push a node void push (Node** head_ref, int new_data) { // Allocate node Node* new_node = new Node(); // Put in the data new_node->data = new_data; // Link the old list of the // new node new_node->next = (*head_ref); // Move the head to point to the // new node (*head_ref) = new_node; } // Function to print linked list void printList(Node *node) { while (node != NULL) { cout << node->data << " "; node = node->next; } cout << endl; } // Driver code int main(void) { Node *head = NULL; push(&head, 0); push(&head, 1); push(&head, 0); push(&head, 2); push(&head, 1); push(&head, 1); push(&head, 2); push(&head, 1); push(&head, 2); cout << "Linked List Before Sorting"; printList(head); sortList(head); cout << "Linked List After Sorting"; printList(head); return 0; } // This code is contributed by rathbhupendra Output: Linked List Before Sorting 2 1 2 1 1 2 0 1 0 Linked List After Sorting 0 0 1 1 1 1 2 2 2 Time Complexity: O(n) where n is the number of nodes in the linked list. Auxiliary Space: O(1) Please refer complete article on Sort a linked list of 0s, 1s and 2s for more details! Comment More infoAdvertise with us Next Article C++ Program For Sorting A Linked List Of 0s, 1s And 2s kartik Follow Improve Article Tags : Linked List Sorting C++ Programs C++ DSA Linked Lists Microsoft Amazon MakeMyTrip Linked-List-Sorting +6 More Practice Tags : AmazonMakeMyTripMicrosoftCPPLinked ListSorting +2 More Similar Reads 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 Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0, 6 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 For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read C++ Program For Writing A Function To Get Nth Node In A Linked List Write a C++ Program to GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before m 4 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 Rearranging A Given Linked List In-Place Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 - 7 min read C++ Program For Insertion Sort In A Singly Linked List We have discussed Insertion Sort for arrays. In this article we are going to discuss Insertion Sort for linked list. Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list. 2) Traverse the given list, do following for every node. ......a) Insert curr 5 min read C++ Program To Merge K Sorted Linked Lists - Set 1 Given K sorted linked lists of size N each, merge them and print the sorted output. Examples: Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10- 6 min read C++ Program For Insertion Sort Insertion sort is a simple sorting algorithm that works by dividing the array into two parts, sorted and unsorted part. In each iteration, the first element from the unsorted subarray is taken and it is placed at its correct position in the sorted array. In this article, we will learn to write a C++ 3 min read Like