Java Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links
Last Updated :
11 Jan, 2022
Given a linked list of 0s, 1s and 2s, sort it.
Examples:
Input: 2->1->2->1->1->2->0->1->0
Output: 0->0->1->1->1->1->2->2->2
The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2.
Input: 2->1->0
Output: 0->1->2
The sorted Array is 0, 1, 2
Method 1: There is a solution discussed in below post that works by changing data of nodes.Â
Sort a linked list of 0s, 1s and 2s
The above solution does not work when these values have associated data with them.Â
For example, these three represent three colors and different types of objects associated with the colors and sort the objects (connected with a linked list) based on colors.
Method 2: In this post, a new solution is discussed that works by changing links.
Approach: Iterate through the linked list. Maintain 3 pointers named zero, one, and two to point to current ending nodes of linked lists containing 0, 1, and 2 respectively. For every traversed node, we attach it to the end of its corresponding list. Finally, we link all three lists. To avoid many null checks, we use three dummy pointers zeroD, oneD, and twoD that work as dummy headers of three lists.
Java
// Java Program to sort a linked list
// 0s, 1s or 2s by changing links
public class Sort012
{
// Sort a linked list of 0s, 1s
// and 2s by changing pointers.
public static Node sortList(Node head)
{
if(head==null || head.next==null)
{
return head;
}
// Create three dummy nodes to point to
// beginning of three linked lists. These
// dummy nodes are created to avoid many
// null checks.
Node zeroD = new Node(0);
Node oneD = new Node(0);
Node twoD = new Node(0);
// Initialize current pointers for three
// lists and whole list.
Node zero = zeroD, one = oneD, two = twoD;
// Traverse list
Node curr = head;
while (curr!=null)
{
if (curr.data == 0)
{
zero.next = curr;
zero = zero.next;
curr = curr.next;
}
else if (curr.data == 1)
{
one.next = curr;
one = one.next;
curr = curr.next;
}
else
{
two.next = curr;
two = two.next;
curr = curr.next;
}
}
// Attach three lists
zero.next = (oneD.next!=null) ?
(oneD.next) : (twoD.next);
one.next = twoD.next;
two.next = null;
// Updated head
head = zeroD.next;
return head;
}
// Function to create and return a node
public static Node newNode(int data)
{
// Allocating space
Node newNode = new Node(data);
newNode.next = null;
return newNode;
}
// Function to print linked list
public static void printList(Node node)
{
while (node != null)
{
System.out.print(node.data+" ");
node = node.next;
}
}
Driver code
public static void main(String args[])
{
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(0);
head.next.next.next = new Node(1);
System.out.println(
"Linked List Before Sorting");
printList(head);
head = sortList(head);
System.out.println(
"Linked List After Sorting");
printList(head);
}
}
class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
}
}
//This code is contributed by Gaurav Tiwari
Output :Â
Linked List Before Sorting
1 2 0 1
Linked List After Sorting
0 1 1 2
Complexity Analysis:Â
- Time Complexity: O(n) where n is a number of nodes in linked list.Â
Only one traversal of the linked list is needed. - Auxiliary Space: O(1).Â
As no extra space is required.
Please refer complete article on
Sort a linked list of 0s, 1s and 2s by changing links for more details!
Similar Reads
Java Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0,
6 min read
Java Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function sh
4 min read
Java Program For Segregating Even And Odd Nodes In A Linked List Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NUL
6 min read
Java Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes,
5 min read
Java Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l
3 min read