In this video, we explore multiple approaches to flattening a linked list of linked lists, where each sub-linked list is sorted. The first method is a simple yet inefficient approach, where we collect all the node values into an array, sort the array, and then create a new linked list by iterating through the sorted array. This method has a time complexity of O(n * m * log(n * m)) and uses O(n * m) space, making it less efficient for large lists.
The second approach uses recursion to merge two sorted linked lists at a time, similar to how we merge lists in merge sort. This results in a time complexity of O(n * n * m), where n is the number of main linked list nodes and m is the size of a single sub-linked list. The third approach optimizes the process by using a Min Heap (priority queue). By always extracting the minimum element from the heap, it ensures that nodes are flattened in sorted order efficiently. This method has a time complexity of O(n * m * log(n)) and uses O(n) space, making it the most efficient. Watch the video to dive deeper into these methods and see them in action!
For more details, please go through - Flattening a Linked List