Advertisement

Merge k Sorted Lists - LeetCode 23 Solution

Merge k Sorted Lists - Complete Solution Guide

Merge k Sorted Lists is LeetCode problem 23, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an array of k linked-lists lists , each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6 Example 2: Input: lists = [] Output: [] Example 3: Input: lists = [[]] Output: [] Constraints: k == lists.length 0 <= k <= 10 4 0 <

Detailed Explanation

The problem asks us to merge k sorted linked lists into one sorted linked list. We are given an array of linked lists, where each linked list is sorted in ascending order. The task is to combine all the elements from these k lists into a single, sorted linked list, and return the head of this merged list. The input can be an empty array of lists, or lists containing empty lists, both of which should return an empty list as the result.

Solution Approach

The provided solution uses a min-heap (priority queue) to efficiently merge the k sorted linked lists. The algorithm initializes the min-heap with the head nodes of each non-empty linked list. Then, it repeatedly extracts the smallest element from the heap, adds it to the merged list, and inserts the next node from the corresponding list into the heap. A dummy head node is used to simplify the construction of the merged list.

Step-by-Step Algorithm

  1. Step 1: Initialize a min-heap (priority queue) to store the head nodes of the k linked lists. Also, use a counter to differentiate between nodes with the same value in the heap and ensure the heap sorts correctly.
  2. Step 2: Iterate through the input array of linked lists. For each non-empty list, add its head node to the min-heap. The min-heap will automatically maintain the smallest element at the top.
  3. Step 3: Create a dummy head node for the merged list. This simplifies the insertion process.
  4. Step 4: While the min-heap is not empty, extract the smallest element (node) from the heap.
  5. Step 5: Append the extracted node to the merged list.
  6. Step 6: If the extracted node has a next node in its original list, add the next node to the min-heap.
  7. Step 7: Repeat steps 4-6 until the min-heap is empty.
  8. Step 8: Return the next node of the dummy head, which is the head of the merged linked list.

Key Insights

  • Insight 1: The key is to efficiently find the smallest element among the heads of the k lists at each step.
  • Insight 2: Using a min-heap (priority queue) is an efficient way to track the smallest elements across all lists.
  • Insight 3: A dummy head node simplifies the process of building the merged linked list.

Complexity Analysis

Time Complexity: O(Nlogk)

Space Complexity: O(k)

Topics

This problem involves: Linked List, Divide and Conquer, Heap (Priority Queue), Merge Sort.

Companies

Asked at: Adobe, Airbnb, Amazon, Anduril, Apple, Bloomberg, ByteDance, Cisco, Citadel, Dell, Deloitte, Docusign, DoorDash, Hubspot, IXL, Indeed, LinkedIn, Meta, Microsoft, MongoDB, NetApp, Nutanix, Nvidia, Nykaa, Oracle, Palantir Technologies, Rivian, Salesforce, Samsung, Snowflake, SoFi, TikTok, Two Sigma, Uber, Verkada, Walmart Labs, Warnermedia, X, Yahoo, Yandex, eBay.