VOOZH about

URL: https://www.geeksforgeeks.org/dsa/flattening-a-linked-list/

⇱ Flattening a Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Flattening a Linked List

Last Updated : 26 Aug, 2025

Given a linked list containing n head nodes where every node in the linked list contains two pointers:

  • next points to the next node in the list.
  • bottom pointer to a sub-linked list where the current node is the head. Each of the sub-linked lists nodes and the head

nodes are sorted in ascending order based on their data .We need to flatten the linked list so that all the nodes appear in a single level while maintaining the sorted order.

Examples:

Input:

👁 1-


Output: 5 -> 7 -> 8 -> 10 -> 19 -> 20 -> 22 -> 28 -> 40 -> 45

Input:

👁 2

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

[Approach 1] Using Sorting - O(n * m * log(n * m)) Time and O(n*m) Space

The idea is to traverse the linked list and push values of all the nodes in an array. Now, we can sort the array in ascending order, and create a new linked list by traversing the sorted array.

Follow the steps to solve the problem:

  • Traverse the entire linked list using:
  • Outer loop → move along the next pointer.
  • Inner loop → move along the bottom pointer.
  • Push every node's data into the array.
  • After traversal, sort the array in ascending order.
  • Now, create a new linked list using the sorted array:
  • Return the bottom of the dummy node — this is the head of the flattened, sorted list.

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

[Approach 2] Using Recursion and Merging list

The idea is similar to Merge two sorted linked lists.

Follow the steps to solve the problem:

  • Using recursion, we first go to the last head node .
  • Then, we keep merging each linked list (connected via bottom pointers) with the result of the already flattened list in sorted order.
  • Move one step back and keep merging the previous list with the merged result.
  • Continue this process until the first list is merged.
  • The final result is a single flattened and sorted list using bottom pointers.

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

Time Complexity: O(n * n * m) - where n is the no of nodes in the main linked list and m is the no of nodes in a single sub-linked list. 

  • After adding the first 2 lists, the time taken will be O(m+m) = O(2m).
  • Then we will merge another list to above merged list -> time = O(2m + m) = O(3m).
  • We will keep merging lists to previously merged lists until all lists are merged.
  • Total time taken will be O(2m + 3m + 4m + .... n*m) = (2 + 3 + 4 + ... + n) * m = O(n * n * m)

Auxiliary Space: O(n), the recursive functions will use a recursive stack of a size equivalent to a total number of nodes in the main linked list.

[Approach 3] Using Priority Queues - O(n * m * log(n)) Time and O(n) Space

The idea is to use a Min Heap, push all the head nodes into it, and always extract the node with the minimum value from the top.

Follow the steps to solve the problem:

  • Create a min-heap with custom comparator.
  • Push all head nodes to heap.
  • Use a dummy node to build the result.
  • While heap is not empty:
  • Pop smallest node.
  • Add it to result.
  • If it has a bottom node, push that into heap.
  • Return dummy node.

Output
5 -> 7 -> 8 -> 10 -> 19 -> 20 -> 22 -> 28 -> 30 -> 50
Comment