Posts

Showing posts from March, 2025

Merge k Sorted Lists

  📌 Problem Idea You are given K sorted lists . You want to merge them into one final sorted list . Instead of merging two lists at a time (which can be slow), you use a min-heap to efficiently find the smallest element among all current nodes. 📚 Steps to Solve Create a min-heap . Each element of the heap will be a pointer to a node. Heap is organized by node->data value (smallest data at top). Insert the first node of each list into the heap. While heap is not empty: Extract the smallest node from the heap (this is the smallest value available). Add this node to the result list. If this extracted node has a next node , insert the next node into the heap. Repeat until all nodes are processed. 🔥 Algorithm Input: Array of K sorted linked list heads Output: Single sorted linked list 1. Initialize an empty Min Heap. 2. For each list: If list is not empty : Insert its head node into the Min Heap. 3. Create a ...