Friday, September 5, 2025
HomeData Modelling & AISort a Linked List of 0s and 1s

Sort a Linked List of 0s and 1s

Given the head of a Linked List of size N, consisting of binary integers 0s and 1s, the task is to sort the given linked list.

Examples:

Input: head = 1 -> 0 -> 1 -> 0 -> 1 -> 0 -> NULLĀ 
Output: 0 -> 0 -> 0 -> 1 -> 1 -> 1 -> NULL

Input: head = 1 -> 1 -> 0 -> NULLĀ 
Output: 0 -> 1 -> 1 -> NULL

Naive Approach: The simplest approach to solve the given problem is to perform the merge sort or insertion sort on the given linked list and sort it. The Implementation of Sorting the Linked list using Merge sort and Sorting the linked list using Insertion sort is discussed already.Ā 

Time complexity: O(N * log N)
Auxiliary Space: O(N)

Better Approach: The simplest approach to solve the given problem can be to traverse the given linked list and store the values in the linked list in an array and then sort the array by using the sort function on array. Then traverse the linked list again and change the values of nodes by the values of array. In this way even the linked lists with values other than 0s and 1s can be sorted. Since the problem states that the linked list initially contain only 0s and 1s, so we can further optimize this approach.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
Ā 
// Creating Link List Node
class Node {
public:
Ā Ā Ā Ā int data;
Ā Ā Ā Ā Node* next;
};
Ā 
// Function to print linked list
void printList(Node* node)
{
Ā Ā Ā Ā // Iterate until node is NOT NULL
Ā Ā Ā Ā while (node != NULL) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Print the data
Ā Ā Ā Ā Ā Ā Ā Ā cout << node->data << " ";
Ā Ā Ā Ā Ā Ā Ā Ā node = node->next; // going to next node
Ā Ā Ā Ā }
}
Ā 
// Function to sort the linked list
// consisting of 0s and 1s
void sortList(Node* head)
{
Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā if ((head == NULL) || (head->next == NULL)) {
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā vector<int> nums;
Ā 
Ā Ā Ā Ā Node* temp = head;
Ā 
Ā Ā Ā Ā while (temp != NULL) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // storing value of node into vector
Ā Ā Ā Ā Ā Ā Ā Ā nums.push_back(temp->data);
Ā Ā Ā Ā Ā Ā Ā Ā // Update the temp node
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->next;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // sorting the nums array
Ā Ā Ā Ā sort(nums.begin(), nums.end());
Ā 
Ā Ā Ā Ā // Update the temp to head
Ā Ā Ā Ā temp = head;
Ā Ā Ā Ā int i = 0;
Ā 
Ā Ā Ā Ā // traversing the linked list
Ā Ā Ā Ā while (i < nums.size()) {
Ā Ā Ā Ā Ā Ā Ā Ā // updating value to nums[i]
Ā Ā Ā Ā Ā Ā Ā Ā temp->data = nums[i];
Ā Ā Ā Ā Ā Ā Ā Ā // point temp to next node and increment i
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->next;
Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā }
}
Ā 
// 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;
}
Ā 
// Driver Code
int main(void)
{
Ā Ā Ā Ā Node* head = NULL;
Ā Ā Ā Ā // pushing values
Ā Ā Ā Ā push(&head, 0);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 0);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 0);
Ā 
Ā Ā Ā Ā // printing linked list before and after sorting
Ā Ā Ā Ā cout << "Linked List (before sorting) : ";
Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā sortList(head);
Ā Ā Ā Ā cout << "\nLinked List (after sorting)Ā  : ";
Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.util.*;
Ā 
class Node {
Ā Ā int data;
Ā Ā Node next;
}
Ā 
class GFG {
Ā 
Ā Ā // Function to print linked list
Ā Ā static void printList(Node node)
Ā Ā {
Ā Ā Ā Ā while (node != null) {
Ā Ā Ā Ā Ā Ā System.out.print(node.data + " ");
Ā Ā Ā Ā Ā Ā node = node.next;
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā // Function to sort the linked list
Ā Ā // consisting of 0s and 1s
Ā Ā static void sortList(Node head)
Ā Ā {
Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā if (head == null || head.next == null) {
Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā List<Integer> nums = new ArrayList<>();
Ā Ā Ā Ā Node temp = head;
Ā 
Ā Ā Ā Ā while (temp != null) {
Ā Ā Ā Ā Ā Ā // storing value of node into list
Ā Ā Ā Ā Ā Ā nums.add(temp.data);
Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // sorting the nums array
Ā Ā Ā Ā Collections.sort(nums);
Ā 
Ā Ā Ā Ā temp = head;
Ā Ā Ā Ā int i = 0;
Ā 
Ā Ā Ā Ā // traversing the linked list
Ā Ā Ā Ā while (i < nums.size()) {
Ā Ā Ā Ā Ā Ā temp.data = nums.get(i);
Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā // Function to push a node
Ā Ā static Node push(Node head, int new_data)
Ā Ā {
Ā Ā Ā Ā Node new_node = new Node();
Ā Ā Ā Ā new_node.data = new_data;
Ā Ā Ā Ā new_node.next = head;
Ā Ā Ā Ā return new_node;
Ā Ā }
Ā 
Ā Ā public static void main(String[] args)
Ā Ā {
Ā Ā Ā Ā Node head = null;
Ā Ā Ā Ā // pushing values
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā 
Ā Ā Ā Ā // printing linked list before and after sorting
Ā Ā Ā Ā System.out.print("Linked List (before sorting) : ");
Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā sortList(head);
Ā Ā Ā Ā System.out.print(
Ā Ā Ā Ā Ā Ā "\nLinked List (after sorting) : ");
Ā Ā Ā Ā printList(head);
Ā Ā }
}
Ā 
// This code is contributed by lokesh.


Python3




class Node:
Ā Ā Ā Ā def __init__(self, data=None):
Ā Ā Ā Ā Ā Ā Ā Ā """
Ā Ā Ā Ā Ā Ā Ā Ā Initialize the node with data and next pointer as None
Ā Ā Ā Ā Ā Ā Ā Ā """
Ā Ā Ā Ā Ā Ā Ā Ā self.data = data
Ā Ā Ā Ā Ā Ā Ā Ā self.next = None
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
def printList(node):
Ā Ā Ā Ā """
Ā Ā Ā Ā Function to print the linked list starting from the provided node
Ā Ā Ā Ā """
Ā Ā Ā Ā # Iterate until node is NOT NULL
Ā Ā Ā Ā while node:
Ā Ā Ā Ā Ā Ā Ā Ā # Print the data
Ā Ā Ā Ā Ā Ā Ā Ā print(node.data, end=' ')
Ā Ā Ā Ā Ā Ā Ā Ā # Go to the next node
Ā Ā Ā Ā Ā Ā Ā Ā node = node.next
Ā 
def sortList(head):
Ā Ā Ā Ā """
Ā Ā Ā Ā Function to sort the linked list consisting of 0s and 1s
Ā Ā Ā Ā """
Ā Ā Ā Ā # Base case
Ā Ā Ā Ā if not head or not head.next:
Ā Ā Ā Ā Ā Ā Ā Ā return
Ā 
Ā Ā Ā Ā nums = []
Ā Ā Ā Ā temp = head
Ā 
Ā Ā Ā Ā # Storing value of node into vector
Ā Ā Ā Ā while temp:
Ā Ā Ā Ā Ā Ā Ā Ā nums.append(temp.data)
Ā Ā Ā Ā Ā Ā Ā Ā # Update the temp node
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next
Ā 
Ā Ā Ā Ā # sorting the nums array
Ā Ā Ā Ā nums.sort()
Ā 
Ā Ā Ā Ā temp = head
Ā Ā Ā Ā i = 0
Ā 
Ā Ā Ā Ā # traversing the linked list
Ā Ā Ā Ā while i < len(nums):
Ā Ā Ā Ā Ā Ā Ā Ā # updating value to nums[i]
Ā Ā Ā Ā Ā Ā Ā Ā temp.data = nums[i]
Ā Ā Ā Ā Ā Ā Ā Ā # point temp to next node and increment i
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next
Ā Ā Ā Ā Ā Ā Ā Ā i += 1
Ā 
def push(head, new_data):
Ā Ā Ā Ā """
Ā Ā Ā Ā Add a new node with provided data to the front of the linked list
Ā Ā Ā Ā """
Ā Ā Ā Ā new_node = Node(new_data)
Ā Ā Ā Ā # Link the old list of the new node
Ā Ā Ā Ā new_node.next = head
Ā Ā Ā Ā return new_node
Ā 
# Driver code
if __name__ == '__main__':
Ā Ā Ā Ā # pushing values
Ā Ā Ā Ā head = None
Ā Ā Ā Ā head = push(head, 0)
Ā Ā Ā Ā head = push(head, 1)
Ā Ā Ā Ā head = push(head, 0)
Ā Ā Ā Ā head = push(head, 1)
Ā Ā Ā Ā head = push(head, 1)
Ā Ā Ā Ā head = push(head, 1)
Ā Ā Ā Ā head = push(head, 1)
Ā Ā Ā Ā head = push(head, 1)
Ā Ā Ā Ā head = push(head, 0)
Ā Ā Ā Ā # printing linked list before and after sorting
Ā Ā Ā Ā print("Linked List (before sorting) : ", end=' ')
Ā Ā Ā Ā printList(head)
Ā Ā Ā Ā sortList(head)
Ā Ā Ā Ā print("\nLinked List (after sorting)Ā  : ", end=' ')
Ā Ā Ā Ā printList(head)


C#




// C# program for the above approach
Ā 
using System;
using System.Collections.Generic;
Ā 
public class GFG {
Ā 
Ā Ā Ā Ā // Structure of a node
Ā Ā Ā Ā class Node {
Ā Ā Ā Ā Ā Ā Ā Ā public int data;
Ā Ā Ā Ā Ā Ā Ā Ā public Node next;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to print linked list
Ā Ā Ā Ā static void PrintList(Node node)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā while (node != null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(node.data + " ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā node = node.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to sort the linked list
Ā Ā Ā Ā // consisting of 0s and 1s
Ā Ā Ā Ā static void SortList(ref Node head)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā Ā Ā Ā Ā if (head == null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā List<int> nums = new List<int>();
Ā Ā Ā Ā Ā Ā Ā Ā Node temp = head;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā while (temp != null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // storing value of node into list
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nums.Add(temp.data);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // sorting the nums array
Ā Ā Ā Ā Ā Ā Ā Ā nums.Sort();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā temp = head;
Ā Ā Ā Ā Ā Ā Ā Ā int i = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // traversing the linked list
Ā Ā Ā Ā Ā Ā Ā Ā while (i < nums.Count) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.data = nums[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to push a node
Ā Ā Ā Ā static Node Push(Node head, int new_data)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Node new_node = new Node();
Ā Ā Ā Ā Ā Ā Ā Ā new_node.data = new_data;
Ā Ā Ā Ā Ā Ā Ā Ā new_node.next = head;
Ā Ā Ā Ā Ā Ā Ā Ā return new_node;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā static public void Main()
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Code
Ā Ā Ā Ā Ā Ā Ā Ā Node head = null;
Ā Ā Ā Ā Ā Ā Ā Ā // pushing values
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = Push(head, 0);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // printing linked list before and after sorting
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write("Linked List (before sorting) : ");
Ā Ā Ā Ā Ā Ā Ā Ā PrintList(head);
Ā Ā Ā Ā Ā Ā Ā Ā SortList(ref head);
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write("\nLinked List (after sorting) : ");
Ā Ā Ā Ā Ā Ā Ā Ā PrintList(head);
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by lokeshmvs21.


Javascript




<script>
Ā 
Ā Ā Ā Ā // JavaScript program for the above approach
Ā 
Ā Ā Ā Ā class Node {
Ā Ā Ā Ā Ā Ā Ā Ā constructor(data) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data = data;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.next = null;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to print linked list
Ā Ā Ā Ā function printList(node) {
Ā Ā Ā Ā Ā Ā Ā Ā let curr = node;
Ā Ā Ā Ā Ā Ā Ā Ā let str = "";
Ā Ā Ā Ā Ā Ā Ā Ā while (curr !== null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā str += curr.data + " ";
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā document.write(str);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to sort the linked list consisting of 0s and 1s
Ā Ā Ā Ā function sortList(head) {
Ā Ā Ā Ā Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā Ā Ā Ā Ā if (!head || !head.next) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā let nums = [];
Ā Ā Ā Ā Ā Ā Ā Ā let curr = head;
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā while (curr !== null) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // storing value of node into list
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā nums.push(curr.data);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // sorting the nums array
Ā Ā Ā Ā Ā Ā Ā Ā nums.sort(function(a, b) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return a - b;
Ā Ā Ā Ā Ā Ā Ā Ā });
Ā Ā Ā Ā Ā Ā Ā Ā curr = head;
Ā Ā Ā Ā Ā Ā Ā Ā let i = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // traversing the linked list
Ā Ā Ā Ā Ā Ā Ā Ā while (i < nums.length) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr.data = nums[i];
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā curr = curr.next;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to push a node
Ā Ā Ā Ā function push(head, newData) {
Ā Ā Ā Ā Ā Ā Ā Ā let newNode = new Node();
Ā Ā Ā Ā Ā Ā Ā Ā newNode.data = newData
Ā Ā Ā Ā Ā Ā Ā Ā newNode.next = head;
Ā Ā Ā Ā Ā Ā Ā Ā return newNode;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā let head = null;
Ā Ā Ā Ā // pushing values
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā 
Ā Ā Ā Ā // printing the linked list before and after sorting
Ā Ā Ā Ā document.write("Linked List (before sorting) : ");
Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā sortList(head);
Ā Ā Ā Ā document.write("<br>Linked List (after sorting) : ");
Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā // This code is contributed by lokeshmvs21.
Ā 
</script>


Output

Linked List (before sorting) : 0 1 1 1 1 1 0 1 0 
Linked List (after sorting)  : 0 0 0 1 1 1 1 1 1 

Time complexity: O(N * log N)
Auxiliary Space: O(N)

Efficient Approach: The above approach can also be optimized by counting the number of 1s and 0s in the given linked list and update the value of nodes accordingly in the linked list. Follow the steps to solve the problem:

  • Traverse the given linked list and store the count of 0s and 1s in variables, say zeroes and ones respectively.
  • Now, traverse the linked list again and change the first zeroes nodes with value 0 and then the remaining nodes with the value 1.
  • After completing the above steps, print the linked list as the resultant sorted list.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
Ā 
#include <bits/stdc++.h>
using namespace std;
Ā 
// Link list node
class Node {
public:
Ā Ā Ā Ā int data;
Ā Ā Ā Ā Node* next;
};
Ā 
// Function to print linked list
void printList(Node* node)
{
Ā Ā Ā Ā // Iterate until node is NOT NULL
Ā Ā Ā Ā while (node != NULL) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Print the data
Ā Ā Ā Ā Ā Ā Ā Ā cout << node->data << " ";
Ā Ā Ā Ā Ā Ā Ā Ā node = node->next;
Ā Ā Ā Ā }
}
Ā 
// Function to sort the linked list
// consisting of 0s and 1s
void sortList(Node* head)
{
Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā if ((head == NULL) || (head->next == NULL)) {
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Store the count of 0s and 1s
Ā Ā Ā Ā int count0 = 0, count1 = 0;
Ā 
Ā Ā Ā Ā // Stores the head node
Ā Ā Ā Ā Node* temp = head;
Ā 
Ā Ā Ā Ā // Traverse the list Head
Ā Ā Ā Ā while (temp != NULL) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // If node->data value is 0
Ā Ā Ā Ā Ā Ā Ā Ā if (temp->data == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment count0 by 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count0++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Otherwise, increment the
Ā Ā Ā Ā Ā Ā Ā Ā // count of 1s
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1++;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Update the temp node
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->next;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Update the temp to head
Ā Ā Ā Ā temp = head;
Ā 
Ā Ā Ā Ā // Traverse the list and update
Ā Ā Ā Ā // the first count0 nodes as 0
Ā Ā Ā Ā while (count0--) {
Ā Ā Ā Ā Ā Ā Ā Ā temp->data = 0;
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->next;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Now, update the value of the
Ā Ā Ā Ā // remaining count1 nodes as 1
Ā Ā Ā Ā while (count1--) {
Ā Ā Ā Ā Ā Ā Ā Ā temp->data = 1;
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp->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;
}
Ā 
// Driver Code
int main(void)
{
Ā Ā Ā Ā Node* head = NULL;
Ā Ā Ā Ā push(&head, 0);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 0);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 1);
Ā Ā Ā Ā push(&head, 0);
Ā Ā Ā Ā cout << "Linked List (before sorting) : ";
Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā sortList(head);
Ā Ā Ā Ā cout << "\nLinked List (after sorting)Ā  : ";
Ā Ā Ā Ā printList(head);
Ā 
Ā Ā Ā Ā return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG{
Ā 
Ā Ā // Link list node
Ā Ā static class Node {
Ā 
Ā Ā Ā Ā int data;
Ā Ā Ā Ā Node next;
Ā Ā };
Ā 
Ā Ā // Function to print linked list
Ā Ā static void printList(Node node)
Ā Ā {
Ā 
Ā Ā Ā Ā // Iterate until node is NOT null
Ā Ā Ā Ā while (node != null) {
Ā 
Ā Ā Ā Ā Ā Ā // Print the data
Ā Ā Ā Ā Ā Ā System.out.print(node.data+ " ");
Ā Ā Ā Ā Ā Ā node = node.next;
Ā Ā Ā Ā }
Ā Ā }
Ā 
Ā Ā // Function to sort the linked list
Ā Ā // consisting of 0s and 1s
Ā Ā static void sortList(Node head)
Ā Ā {
Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā if ((head == null)
Ā Ā Ā Ā Ā Ā Ā Ā || (head.next == null)) {
Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Store the count of 0s and 1s
Ā Ā Ā Ā int count0 = 0, count1 = 0;
Ā 
Ā Ā Ā Ā // Stores the head node
Ā Ā Ā Ā Node temp = head;
Ā 
Ā Ā Ā Ā // Traverse the list Head
Ā Ā Ā Ā while (temp != null) {
Ā 
Ā Ā Ā Ā Ā Ā // If node.data value is 0
Ā Ā Ā Ā Ā Ā if (temp.data == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Increment count0 by 1
Ā Ā Ā Ā Ā Ā Ā Ā count0++;
Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā // Otherwise, increment the
Ā Ā Ā Ā Ā Ā // count of 1s
Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā count1++;
Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā // Update the temp node
Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Update the temp to head
Ā Ā Ā Ā temp = head;
Ā 
Ā Ā Ā Ā // Traverse the list and update
Ā Ā Ā Ā // the first count0 nodes as 0
Ā Ā Ā Ā while (count0>0) {
Ā Ā Ā Ā Ā Ā temp.data = 0;
Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā count0--;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Now, update the value of the
Ā Ā Ā Ā // remaining count1 nodes as 1
Ā Ā Ā Ā while (count1>0) {
Ā Ā Ā Ā Ā Ā temp.data = 1;
Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā count1--;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Print the Linked List
Ā Ā Ā Ā printList(head);
Ā Ā }
Ā 
Ā Ā // Function to push a node
Ā Ā static Node 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;
Ā Ā Ā Ā return head_ref;
Ā Ā }
Ā 
Ā Ā // Driver Code
Ā Ā public static void main(String[] args)
Ā Ā {
Ā Ā Ā Ā Node head = null;
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā sortList(head);
Ā 
Ā Ā }
}
Ā 
// This code is contributed by umadevi9616


Python3




# Python program for the above approach
Ā 
# Link list Node
class Node:
Ā Ā Ā Ā def __init__(self):
Ā Ā Ā Ā Ā Ā Ā Ā self.data = 0;
Ā Ā Ā Ā Ā Ā Ā Ā self.next = None;
Ā 
# Function to print linked list
def printList(Node):
Ā 
Ā Ā Ā Ā # Iterate until Node is NOT None
Ā Ā Ā Ā while (Node != None):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Print data
Ā Ā Ā Ā Ā Ā Ā Ā print(Node.data, end=" ");
Ā Ā Ā Ā Ā Ā Ā Ā Node = Node.next;
Ā Ā Ā Ā Ā 
# Function to sort the linked list
# consisting of 0s and 1s
def sortList(head):
Ā Ā Ā Ā # Base Case
Ā Ā Ā Ā if ((head == None) or (head.next == None)):
Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # Store the count of 0s and 1s
Ā Ā Ā Ā count0 = 0; count1 = 0;
Ā 
Ā Ā Ā Ā # Stores the head Node
Ā Ā Ā Ā temp = head;
Ā 
Ā Ā Ā Ā # Traverse the list Head
Ā Ā Ā Ā while (temp != None):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # If Node.data value is 0
Ā Ā Ā Ā Ā Ā Ā Ā if (temp.data == 0):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # Increment count0 by 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count0 += 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Otherwise, increment the
Ā Ā Ā Ā Ā Ā Ā Ā # count of 1s
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1 += 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Update the temp Node
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # Update the temp to head
Ā Ā Ā Ā temp = head;
Ā 
Ā Ā Ā Ā # Traverse the list and update
Ā Ā Ā Ā # the first count0 Nodes as 0
Ā Ā Ā Ā while (count0 > 0):
Ā Ā Ā Ā Ā Ā Ā Ā temp.data = 0;
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā count0 -= 1;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # Now, update the value of the
Ā Ā Ā Ā # remaining count1 Nodes as 1
Ā Ā Ā Ā while (count1 > 0):
Ā Ā Ā Ā Ā Ā Ā Ā temp.data = 1;
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā count1 -= 1;
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # Print the Linked List
Ā Ā Ā Ā printList(head);
Ā 
# Function to push a Node
def push(head_ref, new_data):
Ā Ā Ā 
Ā Ā Ā Ā # Allocate Node
Ā Ā Ā Ā new_Node = 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;
Ā Ā Ā Ā return head_ref;
Ā 
# Driver Code
if __name__ == '__main__':
Ā Ā Ā Ā head = None;
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā sortList(head);
Ā 
# This code is contributed by umadevi9616


C#




// C# program for the above approach
using System;
Ā 
public class GFG {
Ā 
Ā Ā Ā Ā // Link list node
publicĀ Ā Ā  class Node {
Ā 
Ā Ā Ā Ā publicĀ Ā Ā  int data;
Ā Ā Ā Ā publicĀ Ā Ā  Node next;
Ā Ā Ā Ā };
Ā 
Ā Ā Ā Ā // Function to print linked list
Ā Ā Ā Ā static void printList(Node node) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Iterate until node is NOT null
Ā Ā Ā Ā Ā Ā Ā Ā while (node != null) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Print the data
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(node.data + " ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā node = node.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to sort the linked list
Ā Ā Ā Ā // consisting of 0s and 1s
Ā Ā Ā Ā static void sortList(Node head) {
Ā Ā Ā Ā Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā Ā Ā Ā Ā if ((head == null) || (head.next == null)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Store the count of 0s and 1s
Ā Ā Ā Ā Ā Ā Ā Ā int count0 = 0, count1 = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the head node
Ā Ā Ā Ā Ā Ā Ā Ā Node temp = head;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the list Head
Ā Ā Ā Ā Ā Ā Ā Ā while (temp != null) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If node.data value is 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (temp.data == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment count0 by 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count0++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Otherwise, increment the
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // count of 1s
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the temp node
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Update the temp to head
Ā Ā Ā Ā Ā Ā Ā Ā temp = head;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the list and update
Ā Ā Ā Ā Ā Ā Ā Ā // the first count0 nodes as 0
Ā Ā Ā Ā Ā Ā Ā Ā while (count0 > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.data = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count0--;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Now, update the value of the
Ā Ā Ā Ā Ā Ā Ā Ā // remaining count1 nodes as 1
Ā Ā Ā Ā Ā Ā Ā Ā while (count1 > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.data = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1--;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Print the Linked List
Ā Ā Ā Ā Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to push a node
Ā Ā Ā Ā static Node 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;
Ā Ā Ā Ā Ā Ā Ā Ā return head_ref;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver Code
Ā Ā Ā Ā public static void Main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā Node head = null;
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā sortList(head);
Ā 
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by umadevi9616


Javascript




<script>
// javascript program for the above approach
Ā 
Ā Ā Ā Ā // Link list node
class Node {
Ā Ā Ā Ā constructor() {
Ā Ā Ā Ā Ā Ā Ā Ā this.data = 0;
Ā Ā Ā Ā Ā Ā Ā Ā this.next = null;
Ā Ā Ā Ā }
}
Ā 
Ā Ā Ā Ā // Function to print linked list
Ā Ā Ā Ā function printList(node) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Iterate until node is NOT null
Ā Ā Ā Ā Ā Ā Ā Ā while (node != null) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Print the data
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write(node.data + " ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā node = node.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to sort the linked list
Ā Ā Ā Ā // consisting of 0s and 1s
Ā Ā Ā Ā function sortList(head)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Base Case
Ā Ā Ā Ā Ā Ā Ā Ā if ((head == null) || (head.next == null)) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Store the count of 0s and 1s
Ā Ā Ā Ā Ā Ā Ā Ā var count0 = 0, count1 = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Stores the head node
Ā Ā Ā Ā Ā Ā Ā Ā var temp = head;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the list Head
Ā Ā Ā Ā Ā Ā Ā Ā while (temp != null) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If node.data value is 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (temp.data == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment count0 by 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count0++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Otherwise, increment the
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // count of 1s
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1++;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Update the temp node
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Update the temp to head
Ā Ā Ā Ā Ā Ā Ā Ā temp = head;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Traverse the list and update
Ā Ā Ā Ā Ā Ā Ā Ā // the first count0 nodes as 0
Ā Ā Ā Ā Ā Ā Ā Ā while (count0 > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.data = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count0--;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Now, update the value of the
Ā Ā Ā Ā Ā Ā Ā Ā // remaining count1 nodes as 1
Ā Ā Ā Ā Ā Ā Ā Ā while (count1 > 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp.data = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā count1--;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Print the Linked List
Ā Ā Ā Ā Ā Ā Ā Ā printList(head);
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Function to push a node
Ā Ā Ā Ā function push(head_ref , new_data)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Allocate node
Ā Ā Ā Ā Ā Ā Ā Ā var 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;
Ā Ā Ā Ā Ā Ā Ā Ā return head_ref;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver CodeĀ Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā var head = null;
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 1);
Ā Ā Ā Ā Ā Ā Ā Ā head = push(head, 0);
Ā Ā Ā Ā Ā Ā Ā Ā sortList(head);
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
// This code is contributed by umadevi9616
</script>


Output:Ā 

0 0 0 1 1 1 1 1 1

Ā 

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS