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 lists[0] with lists[1], lists[2] with lists[3], 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.

相关推荐
2501_9403152611 分钟前
leetcode182动态口令(将字符的前几个元素放在字符串后面)
算法
老鼠只爱大米16 分钟前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
疯狂的喵6 小时前
C++编译期多态实现
开发语言·c++·算法
scx201310046 小时前
20260129LCA总结
算法·深度优先·图论
2301_765703146 小时前
C++中的协程编程
开发语言·c++·算法
m0_748708056 小时前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习6 小时前
【算法——c/c++]
c语言·c++·算法
智码未来学堂7 小时前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn7 小时前
基于封装的专项 知识点
java·前端·python·算法
春日见7 小时前
如何避免代码冲突,拉取分支
linux·人工智能·算法·机器学习·自动驾驶