Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Sort Linked List
MEDIUM
20 mins
5 upvotes
Linked List
Sorting
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Linked List
-
-
Sorting
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Sort Linked List

Contributed by
Prateek_18cb
Medium
yellow-spark
0/80
Avg time to solve 20 mins
Success Rate 80 %
Share
5 upvotes

Problem Statement

You are given a linked list of 'N' nodes where nodes can contain values 0, 1, and 2 only. Your task is to sort the linked list.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
0 1 1 2 2 1 1 1 0 1 -1
2 2 2 1 0 -1
Sample Output 1 :
0 0 1 1 1 1 1 1 2 2 
0 1 2 2 2
Explanation of The Sample Input 1 :
For the first test case :
The given linked list is :
0 -> 1 ->  1 ->  2 -> 2 -> 1 -> 1 -> 1 -> 0 -> 1 -> null

Therefore after sorting the list will become :
0 -> 0 ->  1 ->  1 -> 1 -> 1 -> 1 -> 1 -> 2 -> 2 -> null

For the second test case :
The given linked list is :
2 -> 2 ->  2 ->  1 -> 0 -> null

Therefore after sorting the list will become-
0 -> 1 ->  2 ->  2 -> 2 -> null
Sample Input 2 :
3
0 0 0 -1
0 1 1 2 2 -1
2 2 2 2 2 0 -1 
Sample Output 2 :
0 0 0
0 1 1 2 2 
0 2 2 2 2 2
Reset Code
Full screen
Auto
copy-code
Console