C Program For Flattening A Multilevel Linked List
Given a linked list where in addition to the next pointer, each node has a child pointer, which may or may not point to a separate list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in below figure.You are given the head of the first level of the list. Flatten the list so that all the nodes appear in a single-level linked list. You need to flatten the list in way that all nodes at first level should come first, then nodes of second level, and so on.
Each node is a C struct with the following definition.
The above list should be converted to 10->5->12->7->11->4->20->13->17->6->2->16->9->8->3->19->15
The problem clearly says that we need to flatten level by level. The idea of a solution is, we start from the first level, process all nodes one by one, if a node has a child, then we append the child at the end of the list, otherwise, we don’t do anything. After the first level is processed, all next-level nodes will be appended after the first level. The same process is followed for the appended nodes.
1) Take "cur" pointer, which will point to head of the first level of the list 2) Take "tail" pointer, which will point to end of the first level of the list 3) Repeat the below procedure while "curr" is not NULL. I) if current node has a child then a) append this new child list to the "tail" tail->next = cur->child b) find the last node of new child list and update "tail" tmp = cur->child; while (tmp->next != NULL) tmp = tmp->next; tail = tmp; II) move to the next node. i.e. cur = cur->next
Following is the implementation of the above algorithm.
10 5 12 7 11 4 20 13 17 6 2 16 9 8 3 19 15
Time Complexity: Since every node is visited at most twice, the time complexity is O(n) where n is the number of nodes in given linked list.
Space complexity: O(n),where n is the number of nodes in the linked list. This is because the code dynamically allocates memory for each node in the linked list and does not have any additional data structures that would cause an increase in space usage.
Please refer complete article on Flatten a multilevel linked list for more details!
Please Login to comment...