Flatten a Multilevel Doubly Linked List




One of the major benefits of using a Linked List is that they allow the users to insert and delete nodes at any point in time within a program. 


If you have been using the linked lists for quite a while, you might know for a fact that usually the linked lists only have a single level. The levels of the linked lists are constructed of pointers and nodes.


But, sometimes the linked lists might also have different levels which makes them have multiple levels.


In this blog, we are going to be discussing what are multilevel doubly linked lists and how to flatten a linked list so that it appears on a single level.




What is a Linked List?



Linked Lists are one of the common forms of data structures that facilitates storing of objects within nodes.


These objects are stored and initialized at random locations in memory. Each of the nodes of a Linked List consists of two different variables:



  • Data 


This is the actual data stored within the linked list at a specific address location within the memory


  • Pointer


The pointer is used for storing the address for the immediate next node of the linked list


Hence, the structure of a Linked List basically represents a chain between the nodes of the list. These nodes are not essential present in contiguous memory locations like the other forms of data. They can be present at any random location and conjoined together by a link.


This is why the programmers are sometimes required to flatten a linked list in order to form a horizontal linked list structure.


Let us learn in brief detail what flattening a linked list essentially means.




What is a Multilevel Doubly Linked List?



Well before we dive deeper into the concept of flattening a linked list, let's learn more about multilevel doubly linked lists that we are using in the problem statement of the blog.


A multi level linked lists is basically referred to as 2D data structure containing several linked lists in a tiered format.


These linked lists have multiple levels where some of the nodes within the tree point to the next node of a child pointer.


Nearly all the objects stored within a multi level doubly linked list are linked by pointers that contain addresses of the other nodes present in the tree.


The multilevel linked list also contains head nodes, child nodes and even null nodes that do not contain any object or data.



Now, since by flattening a linked list we try to create a single horizontal level of the linked list, when we flatten a Multilevel Doubly Linked List, we essentially try to create a single-level.


This can be achieved by considering each level one at a time and flatten them before moving to the next level. 


In our next section, we have discussed the approaches to flatten a linked list.




How to flatten a Multilevel Doubly Linked List? 



In order to flatten a Multilevel Doubly Linked List, let's have a look at the following problem statement.



Problem Statement



You have been provided a linked list where not only the next pointer but also every other node contains a child pointer. These pointers may or may not be pointing towards an entirely separate list.


These are the children nodes of the string that may contain one or more than one children nodes of their own as well.


This way the entire structure of the multilevel doubly linked list has been formed. You have been provided the head pointer of the first node of the string. 


The task is to flatten the entire list or string such that it appears on a single level. Keep in mind that the nodes on the first level must be placed first, followed by the nodes of the second level and so forth.



Answer Key


As per the problem statement, we have been clearly instructed to flatten the linked list in a leveled order. 


The idea here is that we must begin from the first level itself. While we are on this level, we will process each of the nodes one at a time. 


If a child node is detected, you can append this node towards the end of the level, and if no children nodes are found, the list can be left as it is.


After processing the entire first level, we will have the distance of nearest cell having 1.

This will pave our way to move onto the next level and perform the same actions as we have done for this layer. 


A similar process must be followed to append the child nodes for all the levels of the linked list.



Have a look at the implementation for this approach to flatten a linked list:



  • Start by implementing the utility function in order to form a linked list consisting of n number of nodes


  • The data for this node will be borrowed from the array i.e arr[] 


  • Meanwhile, we will set the value of all our child pointers to Null


  • Next up, initialize the utility function for the second time in order to print all of the nodes from the linked list


  • Depending of the number of levels in the multilevel doubly linked list, create the same number of linked list within your program


  • Modify the children pointers of your list as per their placements


  • Now, once your list is created, return the head pointer. Also, keep in mind that all of the nodes can be reached from head1 */


  • Next up, reach for the tail node of the list and update the tail end to the newly formed last node in your list


  • Finally, change the value of the current node




Wrapping Up


Did you know?


The distance between the cells or nodes of a linked list is not always constant?


Hence, while traversing a linked list, if you have to find the distance of the nearest cell having 1, you will have to reach the head of the list and travel from there. Learn more techniques to traverse a linked list from other blogs on our website. 


Comments

Popular posts from this blog

What are the 5 types of inheritance?

Remove duplicates from a sorted linked list

How often can you Buy and Sell the same stock?