Given a linked list of 0s, 1s and 2s, sort it.

Source: Microsoft Interview | Set 1

Following steps can be used to sort the given linked list.
1) Traverse the list and count the number of 0s, 1s and 2s. Let the counts be n1, n2 and n3 respectively.
2) Traverse the list again, fill the first n1 nodes with 0, then n2 nodes with 1 and finally n3 nodes with 2.

// Program to sort a linked list 0s, 1s or 2s
#include<stdio.h>
#include<stdlib.h>
 
/* Link list node */
struct node
{
     int data;
     struct node* next;
};
 
// Function to sort a linked list of 0s, 1s and 2s
void sortList( struct node *head)
{
     int count[3] = {0, 0, 0};  // Initialize count of '0', '1' and '2' as 0
     struct 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 ( struct node** head_ref, int new_data)
{
     /* allocate node */
     struct node* new_node =
         ( struct node*) malloc ( sizeof ( struct node));
 
     /* put in the data  */
     new_node->data  = new_data;
 
     /* link the old list off 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( struct node *node)
{
     while (node != NULL)
     {
         printf ( "%d  " , node->data);
         node = node->next;
     }
     printf ( "\n" );
}
 
/* Drier program to test above function*/
int main( void )
{
     struct 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);
 
     printf ( "Linked List Before Sorting\n" );
     printList(head);
 
     sortList(head);
 
     printf ( "Linked List After Sorting\n" );
     printList(head);
 
     return 0;
}

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)
Auxiliary Space: O(1)

This article is compiled by Narendra Kangralkar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.