Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptJavascript Program For Sorting A Linked List Of 0s, 1s And 2s

Javascript Program For Sorting A Linked List Of 0s, 1s And 2s

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

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:

Javascript




<script>
// Javascript program to sort aĀ 
// linked list of 0, 1 and 2
Ā Ā 
// Head of list
var head;Ā 
Ā Ā 
// Linked list NodeĀ 
class NodeĀ 
{
Ā Ā Ā Ā constructor(val)Ā 
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā this.data = val;
Ā Ā Ā Ā Ā Ā Ā Ā this.next = null;
Ā Ā Ā Ā }
}
Ā Ā Ā Ā Ā Ā Ā 
function sortList()Ā 
{
Ā Ā Ā Ā // Initialise count of 0 1Ā 
Ā Ā Ā Ā // and 2 as 0
Ā Ā Ā Ā var count = [ 0, 0, 0 ];
Ā Ā 
Ā Ā Ā Ā var 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]++;
Ā Ā Ā Ā Ā Ā Ā Ā ptr = ptr.next;
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā var 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;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā Ā 
// Utility functions
/* Inserts a new Node at frontĀ 
Ā Ā Ā of the list. */
function push(new_data)Ā 
{
Ā Ā Ā Ā /* 1 & 2: Allocate the Node &Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Put in the data */
Ā Ā Ā Ā var new_node = new Node(new_data);
Ā Ā 
Ā Ā Ā Ā // 3. Make next of new Node as headĀ 
Ā Ā Ā Ā Ā Ā Ā Ā new_node.next = head;
Ā Ā 
Ā Ā Ā Ā // 4. Move the head to point toĀ 
Ā Ā Ā Ā // new NodeĀ 
Ā Ā Ā Ā head = new_node;
}
Ā Ā 
// Function to print linked listĀ 
function printList()Ā 
{
Ā Ā Ā Ā var temp = head;
Ā Ā Ā Ā while (temp != null)Ā 
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā document.write(temp.data + " ");
Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā }
Ā Ā Ā Ā document.write("<br/>");
}
Ā Ā 
// Driver code
/* Constructed Linked List is
Ā Ā Ā 1->2->3->4->5->6->7-> 8->8->9->null */
push(0);
push(1);
push(0);
push(2);
push(1);
push(1);
push(2);
push(1);
push(2);
Ā Ā 
document.write(
Ā Ā Ā Ā Ā Ā Ā Ā Ā "Linked List before sorting<br/>");
printList();
Ā Ā 
sortList();
Ā Ā 
document.write(
Ā Ā Ā Ā Ā Ā Ā Ā Ā "Linked List after sorting<br/>");
printList();
// This code is contributed by todaysgaurav
</script>


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!

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

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?