Given a linked list of N nodes where nodes can contain values 0s, 1s and 2s only. The task is to segregate 0s, 1s and 2s linked list such that all zeros segregate to headside, 2s at the end of the linked list and 1s in the mid of 0s and 2s.
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.
For each testcase, segregate the 0s, 1s and 2s and display the linked list.
The task is to complete the function segregate() which segregate the nodes in the linked list as asked in the problem statement. The printing is done automatically by the driver code.
1 <= T <= 100
1 <= N <= 103
The task is to complete the function sortList() which takes head reference as the argument and returns void.
1 2 2 1 2 0 2 2
2 2 0 1
0 1 1 2 2 2 2 2
0 1 2 2
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.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.