LeetCode //C - 23. Merge k Sorted Lists

23. Merge k Sorted Lists

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 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 <= 1 0 4 10^4 104
  • 0 <= listsi.length <= 500
  • − 1 0 4 < = l i s t s i j < = 1 0 4 -10^4 <= listsij <= 10^4 −104<=listsij<=104
  • listsi is sorted in ascending order.
  • The sum of listsi.length will not exceed 1 0 4 10^4 104.

From: LeetCode

Link: 23. Merge k Sorted Lists


Solution:

Ideas:

Main Idea:

  1. Divide: If you have k linked lists, split them into two groups of k/2 linked lists each.
  2. Conquer: Recursively merge the linked lists in each group.
  3. Merge: After the recursive merge, you'll have two merged lists. Now, merge these two lists into one.
    This approach is akin to the merge step in "merge sort" but applied to linked lists.

Detailed Explanation:

  1. mergeTwoLists Function:

    This function is a basic utility to merge two individual sorted linked lists (l1 and l2). If one list is empty, it returns the other. Otherwise, it compares the current nodes' values of the two lists and selects the smaller one, then continues merging with the rest.

  2. mergeKListsHelper Function:

    This is the recursive function that embodies the divide-and-conquer logic:

  • Base Cases:
    • If start > end, there are no lists to merge, so return NULL.
    • If start == end, there's only one list to consider, so return that list.
  • Recursive Divide & Merge:
    • Find the midpoint of the current range of linked lists (mid).
    • Recursively merge the left half (from start to mid).
    • Recursively merge the right half (from mid + 1 to end).
    • Merge the two halves using mergeTwoLists.
  1. mergeKLists Function:
    This is the main function which acts as an initiator. It starts the recursive merge process with the entire range of linked lists (from 0 to listsSize - 1).
Code:
c 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if (!l1) return l2;
    if (!l2) return l1;

    if (l1->val < l2->val) {
        l1->next = mergeTwoLists(l1->next, l2);
        return l1;
    } else {
        l2->next = mergeTwoLists(l1, l2->next);
        return l2;
    }
}

struct ListNode* mergeKListsHelper(struct ListNode** lists, int start, int end) {
    if (start > end) return NULL;
    if (start == end) return lists[start];

    int mid = start + (end - start) / 2;

    struct ListNode* left = mergeKListsHelper(lists, start, mid);
    struct ListNode* right = mergeKListsHelper(lists, mid + 1, end);

    return mergeTwoLists(left, right);
}

struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
    if (listsSize == 0) return NULL;
    return mergeKListsHelper(lists, 0, listsSize - 1);
}
相关推荐
坚持编程的菜鸟6 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
wabs6667 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
坚持编程的菜鸟7 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou7 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
linux-hzh8 小时前
百日算法修炼 · Day 03
java·算法
问商十三载8 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法
不负岁月无痕8 小时前
简单理解操作系统结构
java·linux·c语言·开发语言·c++·面试
qq_448011169 小时前
C语言中的指针函数和函数指针
java·c语言·开发语言
嵌入式老牛9 小时前
三相电气量采集模块设计(三)非同步采样时的精度提升
算法·精度·计量
clerly10 小时前
C语言如何实现继承?
c语言·多态·继承·封装·结构体