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.

相关推荐
智驱力人工智能1 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥2 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风2 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風3 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT063 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠3 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
想进个大厂3 小时前
代码随想录day37动态规划part05
算法
sali-tec3 小时前
C# 基于OpenCv的视觉工作流-章22-Harris角点
图像处理·人工智能·opencv·算法·计算机视觉
子春一4 小时前
Flutter for OpenHarmony:构建一个 Flutter 四色猜谜游戏,深入解析密码逻辑、反馈算法与经典益智游戏重构
算法·flutter·游戏
人道领域4 小时前
AI抢人大战:谁在收割你的红包
大数据·人工智能·算法