# 23. Merge k Sorted Lists

Merge *k* sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

**Example:**

```
Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6
```

```cpp
// Min Heap
ListNode* mergeKLists(vector<ListNode*>& lists) { // time: O(Nlogn); space: O(N), N: total listnodes, n: list number
    auto cmp = [] (ListNode*& a, ListNode*& b) {
        return a->val > b->val;
    };
    // minHeap for ListNode
    priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> pq(cmp);
    for (ListNode*& list : lists) {
        if (list) pq.push(list);
    }
    ListNode *head = nullptr, *pre = nullptr, *tmp = nullptr;
    while (!pq.empty()) {
        tmp = pq.top(); pq.pop();
        if (!pre) head = tmp;
        else pre->next = tmp;
        pre = tmp;
        if (tmp->next) pq.push(tmp->next);
    }
    return head;
}
// struct cmp {
//     bool operator() (ListNode*& a, ListNode*& b) {
//         return a->val > b->val;
//     };
// };
```

```cpp
// Make use of mergeTwoLists()
// Iteration
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { // time: O(m + n); space: O(1)
    ListNode *dummy = new ListNode(-1), *pre = dummy;
    while (l1 && l2) {
        if (l1->val < l2->val) {
            pre->next = l1;
            l1 = l1->next;
        } else {
            pre->next = l2;
            l2 = l2->next;
        }
        pre = pre->next;
    }
    if (l1) pre->next = l1;
    else if (l2) pre->next = l2;
    ListNode* res = dummy->next;
    delete dummy;
    return res;
}
// // Recursion
// ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { // time: O(m + n); space: O(m + n)
//     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;
//     }
// }
ListNode* mergeKLists(vector<ListNode*>& lists) { // time: O(Nlogn)
    if (lists.empty()) return nullptr;
    int n = lists.size();
    while (n > 1) {
        int k = (n + 1) / 2;
        for (int i = 0; i < n / 2; ++i) {
            lists[i] = mergeTwoLists(lists[i], lists[i + k]);
        }
        n = k;
    }
    return lists[0];
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jimmylin1991.gitbook.io/practice-of-algorithm-problems/linked-list/23.-merge-k-sorted-lists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
