Python Coding Practice

Solve Sort a linked list of 0s, 1s and 2s using Python Language

Solve Sort a linked list of 0s, 1s and 2s using Python to enhance your skills with python coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Sort a linked list of 0s, 1s and 2s

Difficulty : Easy

Categories :

  • Linked lists

Given a linked list containing only values 0, 1, and 2, sort it such that all 0s appear at the beginning, followed by all 1s, and then all 2s at the end. Maintain the relative order within each group.

Constraints:

  • 1 ≤ linked list size ≤ 106
  • node->data ∈ {0, 1, 2}

Examples:

Input: LinkedList = 1->2->2->1->2->0->2->2
Output: 0->1->1->2->2->2->2->2
Explanation: After sorting, 0 appears first, followed by 1s, and then all 2s.
Input: LinkedList = 2->2->0->1
Output: 0->1->2->2
Explanation: Single 0 comes first, followed by single 1, and then two 2s.

Note:

Can you solve it in a single pass through the linked list?

Problem Solving

Input

What You'll Find Here

Interactive Exercises Practice coding with problems designed for beginners and experts.

Step-by-Step Solutions Understand every step of the solution process.

Real-World Scenarios Apply your skills to real-world problems and boost your confidence.

Choose from the following categories