LeetCode 23: Merge k Sorted Lists

LeetCode 23: Merge k Sorted Lists

    • [1. 📌 Problem Links](#1. 📌 Problem Links)
    • [2. 🧠 Solution Overview](#2. 🧠 Solution Overview)
    • [3. 🟢 Solution 1: Min-Heap (Priority Queue)](#3. 🟢 Solution 1: Min-Heap (Priority Queue))
      • [3.1. Algorithm Idea](#3.1. Algorithm Idea)
      • [3.2. Key Points](#3.2. Key Points)
      • [3.3. Java Implementation](#3.3. Java Implementation)
      • [3.4. Complexity Analysis](#3.4. Complexity Analysis)
    • [4. 🟡 Solution 2: Divide and Conquer (Merge Sort Style)](#4. 🟡 Solution 2: Divide and Conquer (Merge Sort Style))
      • [4.1. Algorithm Idea](#4.1. Algorithm Idea)
      • [4.2. Key Points](#4.2. Key Points)
      • [4.3. Java Implementation](#4.3. Java Implementation)
      • [4.4. Complexity Analysis](#4.4. Complexity Analysis)
    • [5. 🔵 Solution 3: Iterative Merge (Bottom-Up)](#5. 🔵 Solution 3: Iterative Merge (Bottom-Up))
      • [5.1. Algorithm Idea](#5.1. Algorithm Idea)
      • [5.2. Key Points](#5.2. Key Points)
      • [5.3. Java Implementation](#5.3. Java Implementation)
      • [5.4. Complexity Analysis](#5.4. Complexity Analysis)
    • [6. 📊 Solution Comparison](#6. 📊 Solution Comparison)
    • [7. 💡 Summary](#7. 💡 Summary)

2. 🧠 Solution Overview

This problem requires merging k sorted linked lists into one sorted linked list. The challenge is to do this efficiently, especially when k is large. Below are the main approaches:

Method Key Idea Time Complexity Space Complexity
Min-Heap (Priority Queue) Always extract minimum node O(n log k) O(k)
Divide and Conquer Merge lists in pairs recursively O(n log k) O(log k)
Brute Force Merge lists one by one O(nk) O(1)

3. 🟢 Solution 1: Min-Heap (Priority Queue)

3.1. Algorithm Idea

We use a min-heap (priority queue) to always get the smallest node among all current list heads. We repeatedly extract the smallest node, add it to the result list, and push its next node back into the heap if it exists.

3.2. Key Points

  • Heap Initialization: Add the first node of each list to the heap
  • Extract Minimum: Heap always gives us the smallest current node
  • Incremental Addition: After extracting a node, add its next node to heap
  • Dummy Node: Use dummy node to simplify list construction
  • Termination: Stop when heap is empty

3.3. Java Implementation

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        
        PriorityQueue<ListNode> minHeap = new PriorityQueue<>((a, b) -> a.val - b.val);
        
        // Add first node of each list to the heap
        for (ListNode list : lists) {
            if (list != null) {
                minHeap.offer(list);
            }
        }
        
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        
        while (!minHeap.isEmpty()) {
            ListNode smallest = minHeap.poll();
            current.next = smallest;
            current = current.next;
            
            // Add next node from the same list to heap
            if (smallest.next != null) {
                minHeap.offer(smallest.next);
            }
        }
        
        return dummy.next;
    }
}

3.4. Complexity Analysis

  • Time Complexity : O(n log k) - Each of n nodes is inserted/extracted from heap of size k
  • Space Complexity : O(k) - Heap stores at most k nodes

4. 🟡 Solution 2: Divide and Conquer (Merge Sort Style)

4.1. Algorithm Idea

This approach treats the problem like merge sort. We recursively divide the k lists into halves, merge each half, and then merge the two halves together. This reduces the problem to multiple instances of merging two sorted lists.

4.2. Key Points

  • Recursive Division: Split lists array into left and right halves
  • Base Case: When only one list remains, return it
  • Merge Two Lists: Use standard two-list merging
  • Bottom-up Approach: Merge smallest pairs first

4.3. Java Implementation

java 复制代码
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        return mergeLists(lists, 0, lists.length - 1);
    }
    
    private ListNode mergeLists(ListNode[] lists, int left, int right) {
        if (left == right) {
            return lists[left];
        }
        
        int mid = left + (right - left) / 2;
        ListNode leftList = mergeLists(lists, left, mid);
        ListNode rightList = mergeLists(lists, mid + 1, right);
        
        return mergeTwoLists(leftList, rightList);
    }
    
    private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }
        
        // Attach remaining nodes
        if (l1 != null) {
            current.next = l1;
        } else {
            current.next = l2;
        }
        
        return dummy.next;
    }
}

4.4. Complexity Analysis

  • Time Complexity : O(n log k) - Each node is merged log k times
  • Space Complexity : O(log k) - Recursion stack depth

5. 🔵 Solution 3: Iterative Merge (Bottom-Up)

5.1. Algorithm Idea

This is an iterative version of the divide and conquer approach. We merge lists in pairs repeatedly until only one list remains. This avoids recursion overhead and uses constant space.

5.2. Key Points

  • Pairwise Merging: Merge lists0 with lists1, lists2 with lists3, etc.
  • In-place Update: Store merged lists back in the array
  • Halving Strategy: Each iteration reduces number of lists by half
  • Iterative Control: Use while loop until only one list remains

5.3. Java Implementation

java 复制代码
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        
        int n = lists.length;
        
        while (n > 1) {
            for (int i = 0; i < n / 2; i++) {
                lists[i] = mergeTwoLists(lists[i], lists[n - 1 - i]);
            }
            n = (n + 1) / 2; // Handle odd number of lists
        }
        
        return lists[0];
    }
    
    private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }
        
        if (l1 != null) current.next = l1;
        else current.next = l2;
        
        return dummy.next;
    }
}

5.4. Complexity Analysis

  • Time Complexity : O(n log k) - Same as recursive approach
  • Space Complexity : O(1) - No recursion stack, only iterative merging

6. 📊 Solution Comparison

Solution Time Space Pros Cons
Min-Heap O(n log k) O(k) Simple, intuitive Extra space for heap
Divide & Conquer O(n log k) O(log k) Optimal time, elegant Recursion overhead
Iterative Merge O(n log k) O(1) No recursion, optimal space More complex implementation

7. 💡 Summary

For the Merge k Sorted Lists problem:

  • Recommended Approach : Min-Heap is most intuitive and commonly used in interviews
  • Production Use : Divide and Conquer offers better space efficiency for large k
  • Key Insight: The problem can be reduced to repeatedly finding the minimum element or merging in a balanced way

The min-heap approach is particularly elegant because it abstracts away the complexity of comparing across multiple lists and always gives us the next smallest element efficiently.

Merging multiple sorted lists teaches us that complex coordination problems can often be solved by systematically identifying and processing the most promising next step - much like using a priority queue to always work on the most urgent task first.

相关推荐
rannn_111几秒前
【力扣hot100】多维动态规划|62、64、5、1143、72
算法·leetcode·动态规划
吴声子夜歌10 分钟前
ApacheCommons——commons-math3(科学计算与线性代数)(一)
java·线性代数·算法·apache
tachibana211 分钟前
Embedding 有哪几种算法?
人工智能·算法·ai·大模型·llm·embedding·agent
吴声子夜歌39 分钟前
ApacheCommons——commons-configuration2(多数据源配置统一管理与热加载)
java·开发语言·算法·apache
ychqsq40 分钟前
159.夜思
经验分享·职场和发展
kyle~40 分钟前
奇异值分解(SVD)
人工智能·算法·机器学习
HugoStudio_SWAN41 分钟前
洛谷 B4502 [GESP202603 四级] 礼盒排序——多关键字排序与排名计数法
算法
计科杨某人1 小时前
简单算法题(基础入门题)
c++·算法·题解·入门·基础算法
Q一件事1 小时前
RWEQ计算——Wf因子
算法
zander2581 小时前
LeetCode 64. 最小路径和
算法·深度优先