- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# JavaScript Program for Sorting a Linked List of 0s, 1s, And 2s

In this tutorial, we will learn the JavaScript program for sorting a linked list of 0s, 1s, and 2s. Sorting algorithms are essential for any programming language, and JavaScript is no exception. Sorting a linked list of 0s, 1s, and 2s is a common problem that developers encounter in coding interviews and real-world applications.

So, let's dive in and explore how to sort a linked list of 0s, 1s, and 2s using JavaScript programming.

## What is sorting?

Sorting is the process of arranging elements in a specific order, either ascending or descending. It is a fundamental operation in computer science and has numerous applications in real-world scenarios. Sorting algorithms are used to organize data for efficient search, reduce redundancy, and optimize space and time complexity.

Here are some examples of sorting in JavaScript:

**Example 1** − Sorting an array of numbers in ascending order:

Input: ar[]= [5, 3, 8, 1, 2, 9] Output: [1, 2, 3, 5, 8, 9]

**Example 2** − Sorting an array of strings in alphabetical order:

Input: ['apple', 'banana', 'orange', 'grape'] Output: ['apple', 'banana', 'grape', 'orange']

## What is a linked list?

A linked list is a linear data structure consisting of nodes that are linked together by pointers. Each node contains a data element and a reference to the next node in the list. Linked lists are commonly used for dynamic data structures, where the size of the data changes frequently.

## Problem Statement

The objective is to arrange and display a linked list comprising 0s, 1s, and 2s in an ordered sequence. Let’s understand it with examples:

### 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

## Algorithm for Sorting a Linked List of 0s, 1s, and 2s

The steps for sorting a linked list of 0s, 1s, and 2s using the counting sort algorithm −

**STEP 1** − Define a function sortList(head) which takes the head of the linked list as input.

**STEP2** − Initialize a count array count[] of size 3 with all elements as 0.

**STEP 3** − Traverse the linked list and increment the count of the node data at the corresponding index in the count array.

**STEP 4** − Traverse the linked list again and replace the node data with the lowest index value for which the count is greater than 0.

**STEP 5** − Decrement the count of the node data for each replacement.

**STEP 6** − Print the linked list before and after sorting.

Now let’s try to understand the above algorithm with an example where we implement this algorithm using JavaScript.

## Example

The below JavaScript program uses a counting sort algorithm to sort a linked list containing 0s, 1s, and 2s. The algorithm first counts the frequency of 0s, 1s, and 2s in the list, then updates the values of nodes in the list based on the count of each value.

/* Link list node */ class Node { constructor(data) { this.data = data; this.next = null; } } class LinkedList { constructor() { this.head = null; } push(new_data) { const new_node = new Node(new_data); new_node.next = this.head; this.head = new_node; } printList() { let currentNode = this.head; let value = ""; while (currentNode !== null) { value += currentNode.data + " -> "; currentNode = currentNode.next; } console.log(value + "null"); } sortList() { const count = [0, 0, 0]; // Initialize count of '0', '1' and '2' as 0 let ptr = this.head; while (ptr !== null) { count[ptr.data] += 1; ptr = ptr.next; } ptr = this.head; let i = 0; while (ptr !== null) { if (count[i] === 0) { ++i; } else { ptr.data = i; --count[i]; ptr = ptr.next; } } } } const linkedList = new LinkedList(); linkedList.push(0); linkedList.push(1); linkedList.push(0); linkedList.push(2); linkedList.push(1); linkedList.push(1); linkedList.push(2); linkedList.push(1); linkedList.push(2); console.log("Before sorting:"); linkedList.printList(); linkedList.sortList(); console.log("After sorting:"); linkedList.printList();

## Conclusion

Overall, the above Javascript program demonstrates an efficient method for sorting a linked list that contains only 0s, 1s, and 2s, using the counting technique. The algorithm has a time complexity of O(n) and a space complexity of O(1), making it an optimal solution for this particular sorting problem.