Sort Linked List Of 0s 1s And 2s

easy
1. You are given a linked list with N nodes where a node can contain values
       0, 1 or 2.
    2. You have to write a function that segregate 0s, 1s and 2s in the linked
       list such that all zeros appear to headside, 2s at the end of the linked
       list and 1s in the middle of 0s and 2s.
    3. display is a utility function which displays the contents of Linked List,
       feel free to use it for debugging purposes.
    4. main takes input from the users and creates the Linked List. You can use
       display to know its contents.
    5. This is a functional problem. 
    6. You should code only the linksort function. It takes as input the
       head of the linked list.
    7. Don't change the code of Node, main and display.

Input Format

First line takes N, the number of elements in the list. Next line takes input N space separated numbers reperesenting elements of the linked list. Input is handled for you.

Output Format

Segregated Linked List. Output is handled for you.

Constraints

1 <= N <= 1000

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
8
1 2 2 1 2 0 2 2
Output
0 1 1 2 2 2 2 2
Previous
Reverse Linked List In Groups Of K
Next
Split Linked List In K Parts

Related Questions