Given a linked list of 0s, 1s and 2s, sort it.
Submissions: 13299   Accuracy:

49.37%

  Difficulty: Easy   Marks: 2

Given a linked list of size N consisting of 0s, 1s and 2s. The task os to sort this linked list such that all zeroes segregate to headside, 2s at the end and 1s in the mid of 0s and 2s.

Input:
First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list and next line contains N elements as the data in the linked list.

Output:
For each testcase, segregate the 0s, 1s and 2s and display the linked list.

Constraints:
1 <= T <= 100
1 <= N <= 103

User Task:
The task is to complete the function sortList() which takes head reference as the argument and returns void.

Example:
Input:
1
8
1 2 2 1 2 0 2 2

Output:
0 1 1 2 2 2 2 2 

Explanation:
Testcase 1:
All the 0s are segregated to left end of the linked list, 2s to the right end of the list and 1s in between.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Jaspreet Singh


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.