Given a linked list where every node represents a linked list and contains two pointers of its type:

- Pointer to next node in the main list (we call it ‘right’ pointer in below code)
- Pointer to a linked list where this node is head (we call it ‘down’ pointer in below code).

All linked lists are sorted. See the following example

**Examples:**

Input:5 -> 10 -> 19 -> 28 | | | | V V V V 7 20 22 35 | | | V V V 8 50 40 | | V V 30 45Output:5->7->8->10->19->20->22->28->30->35->40->45->50Input:5 -> 10 -> 19 -> 28 | | V V 7 22 | | V V 8 50 | V 30Output:5->7->8->10->19->20->22->30->50

In the previous post, we have use the **merge()** process of merge sort for linked lists to flatten the linked list.

In this post, we will solve it using Heap.

**Approach:** The idea is to observe that from each top node there are **N** nodes that are connected in a downward direction but observe that all the downward nodes are in sorted order. So the task is to sort this entire thing in increasing order(or decreasing order).

- Push the head of all the linked list in the downward list in priority queue.
- Pop the smallest node from the priority queue.
- Check the location of node so that next node pointed by the current node can be pushed into the priority queue.
- Again pop out the smallest element and insert next node pointed by current node till the heap becomes empty.
- Keep on adding the data of nodes in a new linked list that are pooped out to the new list.
- Print the linked list formed above.

Below is the implementation of the above approach:

`// C++ program for Flattening ` `// a linked list using Heaps ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure of given Linked list ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* right; ` ` ` `struct` `Node* down; ` ` ` ` ` `Node(` `int` `x) ` ` ` `{ ` ` ` `data = x; ` ` ` `right = NULL; ` ` ` `down = NULL; ` ` ` `} ` `}; ` ` ` `// Function to print the ` `// linked list ` `void` `printList(Node* Node) ` `{ ` ` ` `while` `(Node != NULL) { ` ` ` `printf` `(` `"%d "` `, Node->data); ` ` ` `Node = Node->down; ` ` ` `} ` `} ` ` ` `// Function that compares the value ` `// pointed by node and returns true ` `// if first data is greater ` `struct` `compare { ` ` ` `bool` `operator()(Node* a, Node* b) ` ` ` `{ ` ` ` `return` `a->data > b->data; ` ` ` `} ` `}; ` ` ` `// Function which returns the root ` `// of the flattened linked list ` `Node* flatten(Node* root) ` `{ ` ` ` `Node* ptr = root; ` ` ` `Node* head = NULL; ` ` ` ` ` `// Min Heap which will return ` ` ` `// smallest element currently ` ` ` `// present in heap ` ` ` `priority_queue<Node*, ` ` ` `vector<Node*>, ` ` ` `compare> ` ` ` `pq; ` ` ` ` ` `// Push the head nodes of each ` ` ` `// downward linked list ` ` ` `while` `(ptr != NULL) { ` ` ` `pq.push(ptr); ` ` ` `ptr = ptr->right; ` ` ` `} ` ` ` ` ` `// This loop will execute ` ` ` `// till the map becomes empty ` ` ` `while` `(!pq.empty()) { ` ` ` ` ` `// Pop out the node that ` ` ` `// contains element ` ` ` `// currently in heap ` ` ` `Node* temp = pq.top(); ` ` ` `pq.pop(); ` ` ` ` ` `// Push the next node pointed by ` ` ` `// the current node into heap ` ` ` `// if it is not null ` ` ` `if` `(temp->down != NULL) { ` ` ` `pq.push(temp->down); ` ` ` `} ` ` ` ` ` `// Create new linked list ` ` ` `// that is to be returned ` ` ` `if` `(head == NULL) { ` ` ` `head = temp; ` ` ` `ptr = temp; ` ` ` `} ` ` ` `else` `{ ` ` ` `ptr->down = temp; ` ` ` `ptr = temp; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Pointer to head node ` ` ` `// in the linked list ` ` ` `return` `head; ` `} ` ` ` `// Create and push new nodes ` `void` `push(Node** head_ref, ` ` ` `int` `new_data) ` `{ ` ` ` `Node* new_node = (Node*)` `malloc` `(` `sizeof` `(Node)); ` ` ` `new_node->right = NULL; ` ` ` `new_node->data = new_data; ` ` ` `new_node->down = (*head_ref); ` ` ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `Node* root = NULL; ` ` ` ` ` `// Given Linked List ` ` ` `push(&root, 30); ` ` ` `push(&root, 8); ` ` ` `push(&root, 7); ` ` ` `push(&root, 5); ` ` ` ` ` `push(&(root->right), 20); ` ` ` `push(&(root->right), 10); ` ` ` ` ` `push(&(root->right->right), 50); ` ` ` `push(&(root->right->right), 22); ` ` ` `push(&(root->right->right), 19); ` ` ` ` ` `push(&(root->right->right->right), 45); ` ` ` `push(&(root->right->right->right), 40); ` ` ` `push(&(root->right->right->right), 35); ` ` ` `push(&(root->right->right->right), 20); ` ` ` ` ` `// Flatten the list ` ` ` `root = flatten(root); ` ` ` ` ` `// Print the flatened linked list ` ` ` `printList(root); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5 7 8 10 19 20 20 22 30 35 40 45 50

**Time Complexity:** *O(N)*, where N is number of node is the linked list.

**Auxiliary Space:** *O(N)*, where N is number of node is the linked list.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Flattening a Linked List
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- Check if a linked list is Circular Linked List
- Convert Singly Linked List to XOR Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Construct a Doubly linked linked list from 2D Matrix
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Length of longest palindrome list in a linked list using O(1) extra space
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Linked List | Set 1 (Introduction)
- Can we reverse a linked list in less than O(n)?
- Linked List Implementation in C#
- Linked List Sum of Nodes Between 0s
- Generic Linked List in C

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.