Write Python Code to Flattening the Linked List

In this tutorial, we will write the Python code to flattening the given linked list. A given linked list which consists of every node represents a linked list and contains two pointers of its type.

The first pointer represents the next pointer to the next node and a bottom pointer to a linked list where this node is head. Each of the sub-linked list in sorted order.

Here the flattening means that all nodes must appear in a single level while maintaining the sorted order.

Let's see the problem statement.

Problem Statement

Example -

Input:

Output:

5-> 7-> 8- > 10 -> 19-> 20-> 22-> 28-> 30-> 35-> 40-> 45-> 50.

Example - 2:

Input:

Output:

5->7->8->10->19->22->28->30->50

Solution

In this method, first we will convert into the regular list and then flatten the elements. Let's understand the following example.

Example -

Output:

Flattened linked list: [5, 7, 8, 30, 10, 19, 28, 22, 50]

Explanation -

In the above code, first we created the node with the next and next to next pointer. We created the flatten_linked_list() that takes the head of the linked list as input. It converts the linked list into a regular list by traversing the linked list and appending the data of each node to the list. Then, it flattens the list by recursively calling the flatten_linked_list function on any child nodes encountered. Finally, it returns the flattened list.

The program creates the example linked list as described and then calls the flatten_linked_list() function on the head of the linked list. The resulting flattened list is printed as the output.

Example -

Output:

Flattened linked list: [5, 7, 8, 30, 10, 19, 28, 22, 50]

Explanation -

In the above code, we create a Node class which represents a node in the linked list. Each node has a data attribute to store the value and two pointers: right and down. The right pointer is used to connect nodes horizontally, and the down pointer is used to connect nodes vertically.

The code also defines a LinkedList class, which has methods to insert nodes at the beginning of the list, print the list, and flatten the list.

In the LinkedList class, the insert_at_beginning() method takes a reference to the head node (head_ref) and data as input. It creates a new node with the given data and sets its down pointer to the current head. Then, it updates the head_ref to point to the new node, effectively inserting the new node at the beginning of the list. The updated head_ref is returned.

The print_list() method traverses the linked list and prints the data of each node, moving vertically using the down pointer until the end of the list is reached.

The merge_lists() method is a recursive function that takes two lists (a and b) as input and merges them into a single sorted list.

It compares the data of the nodes at the head of each list and recursively merges the remaining lists.

The merging is done by updating the down pointer of the current node to the result of the recursive call with the remaining nodes. The right pointer of the current node is set to None to ensure a flattened structure.

The flatten() method is the main function that flattens the given multilevel linked list. It takes the root node as input and recursively flattens the list by following these steps:

If the root is None or there are no nodes to the right (root.right is None), it returns the root.

It recursively calls flatten on the right sublist to flatten it.

Then, it merges the current root with the flattened right sublist using the merge_lists method.

Finally, it returns the flattened root. Then, an instance of the LinkedList class is created.

Nodes are inserted at the beginning of the list using the insert_at_beginning() method, creating a multilevel structure.

The list is flattened by calling the flatten method on the head of the linked list.

Finally, the flattened list is printed using the print_list method.






Latest Courses