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 -> NULLInput: 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 Nodeclass Node {public:Ā Ā Ā Ā int data;Ā Ā Ā Ā Node* next;};Ā
// Function to print linked listvoid 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 1svoid 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 nodevoid 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 Codeint 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 approachimport 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 codeif __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> |
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 nodeclass Node {public:Ā Ā Ā Ā int data;Ā Ā Ā Ā Node* next;};Ā
// Function to print linked listvoid 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 1svoid 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 nodevoid 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 Codeint 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 approachimport 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 Nodeclass Node: Ā Ā Ā Ā def __init__(self):Ā Ā Ā Ā Ā Ā Ā Ā self.data = 0; Ā Ā Ā Ā Ā Ā Ā Ā self.next = None;Ā
# Function to print linked listdef 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 1sdef 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 Nodedef 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 Codeif __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 approachusing System;Ā
public class GFG {Ā
Ā Ā Ā Ā // Link list nodepublicĀ Ā Ā 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 nodeclass 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> |
0 0 0 1 1 1 1 1 1
Ā
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Info to that Topic: geeksforgeeks.org/sort-a-linked-list-of-0s-and-1s/ […]