Flattening of LL

Linked-List FAQs (Hard) Hard

Given a special 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
  • ‘Child’ pointer to a linked list where the current node is the head

Each of these child linked lists is in sorted order and connected by a 'child' pointer.


Flatten this linked list such that all nodes appear in a single sorted layer connected by the 'child' pointer and return the head of the modified list.

Constraints

  • n == Number of head nodes
  • 1 <= n <= 100
  • 1 <= Number of nodes in each child linked list <= 100
  • 0 <= ListNode.val <= 1000
  • All child linked lists are sorted in non-decreasing order

Hints

  • A brute-force approach would: Extract all nodes into an array (O(n) space). Sort the array (O(N log N) time). Reconstruct the list using the child pointer.
  • Since each child list is already sorted, we can efficiently merge them using: Priority Queue / Min-Heap (O(N log k)) Iterative Merging (Similar to Merge k Sorted Lists in Linked Lists) (O(N log k)) Divide and Conquer (Merging Pairs of Lists Efficiently) (O(N log k))

Company Tags

GE Healthcare Western Digital Riot Games Alibaba Optum Unity Technologies Bloomberg KPMG PwC Morgan Stanley HCL Technologies DoorDash Siemens Healthineers IBM Airbnb Robinhood Walmart Docker Broadcom Intel Roche Goldman Sachs Rockstar Games Zoho Snowflake Google Microsoft Amazon Meta Apple Netflix Adobe