Open In App

# Python Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links

Given a linked list of 0s, 1s and 2s, sort it.
Examples:

```Input: 2->1->2->1->1->2->0->1->0
Output: 0->0->1->1->1->1->2->2->2
The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2.

Input: 2->1->0
Output: 0->1->2
The sorted Array is 0, 1, 2```

Method 1: There is a solution discussed in below post that works by changing data of nodes.
Sort a linked list of 0s, 1s and 2s
The above solution does not work when these values have associated data with them.
For example, these three represent three colors and different types of objects associated with the colors and sort the objects (connected with a linked list) based on colors.

Method 2: In this post, a new solution is discussed that works by changing links.
Approach: Iterate through the linked list. Maintain 3 pointers named zero, one, and two to point to current ending nodes of linked lists containing 0, 1, and 2 respectively. For every traversed node, we attach it to the end of its corresponding list. Finally, we link all three lists. To avoid many null checks, we use three dummy pointers zeroD, oneD, and twoD that work as dummy headers of three lists.

## Python3

 `# Python3 Program to sort a linked list ``# 0s, 1s or 2s by changing links``import` `math`` ` `# Link list node ``class` `Node: ``    ``def` `__init__(``self``, data): ``        ``self``.data ``=` `data ``        ``self``.``next` `=` `None`` ` `#Node* newNode( data)`` ` `# Sort a linked list of 0s, 1s ``# and 2s by changing pointers.``def` `sortList(head):``    ``if` `(head ``=``=` `None` `or` `        ``head.``next` `=``=` `None``):``        ``return` `head`` ` `    ``# Create three dummy nodes to point ``    ``# to beginning of three linked lists. ``    ``# These dummy nodes are created to ``    ``# avoid many None checks.``    ``zeroD ``=` `Node(``0``)``    ``oneD ``=` `Node(``0``)``    ``twoD ``=` `Node(``0``)`` ` `    ``# Initialize current pointers for ``    ``# three lists and whole list.``    ``zero ``=` `zeroD ``    ``one ``=` `oneD ``    ``two ``=` `twoD`` ` `    ``# Traverse list``    ``curr ``=` `head``    ``while` `(curr):``        ``if` `(curr.data ``=``=` `0``):``            ``zero.``next` `=` `curr``            ``zero ``=` `zero.``next``            ``curr ``=` `curr.``next``        ``elif``(curr.data ``=``=` `1``):``            ``one.``next` `=` `curr``            ``one ``=` `one.``next``            ``curr ``=` `curr.``next``        ``else``:``            ``two.``next` `=` `curr``            ``two ``=` `two.``next``            ``curr ``=` `curr.``next``         ` `    ``# Attach three lists``    ``zero.``next` `=` `(oneD.``next``) ``if` `(oneD.``next``) ``                            ``else` `(twoD.``next``)``    ``one.``next` `=` `twoD.``next``    ``two.``next` `=` `None`` ` `    ``# Updated head``    ``head ``=` `zeroD.``next`` ` `    ``# Delete dummy nodes``    ``return` `head`` ` `# Function to create and return ``# a node``def` `newNode(data):``     ` `    ``# Allocating space``    ``newNode ``=` `Node(data)`` ` `    ``# Inserting the required data``    ``newNode.data ``=` `data``    ``newNode.``next` `=` `None``    ``return` `newNode`` ` `# Function to print linked list ``def` `prList(node):``    ``while` `(node !``=` `None``):``        ``print``(node.data, end ``=` `" "``)``        ``node ``=` `node.``next``     ` `# Driver Code``if` `__name__``=``=``'__main__'``:``     ` `    ``# Creating the list 1.2.4.5``    ``head ``=` `newNode(``1``)``    ``head.``next` `=` `newNode(``2``)``    ``head.``next``.``next` `=` `newNode(``0``)``    ``head.``next``.``next``.``next` `=` `newNode(``1``)`` ` `    ``print``(``"Linked List Before Sorting"``)``    ``prList(head)`` ` `    ``head ``=` `sortList(head)`` ` `    ``print``(``"Linked List After Sorting"``)``    ``prList(head)``# This code is contributed by Srathore`

Output :

```Linked List Before Sorting
1  2  0  1