> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/23.-merge-k-sorted-lists-hard.md).

# 23. Merge k Sorted Lists

## 1.問題&#x20;

* Merge多個sorted list並回傳完整的sorted list.&#x20;
* 分析並解釋他的Complexity

![](/files/-LHMVSy0PUMpSDZN7N4k)

## 2.想法&#x20;

* Merge sort, 省略掉split的部分, 將每個sorted list當成最小的單位

## 3.程式碼&#x20;

```
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* merge(ListNode* l1, ListNode *l2){
        if(!l1){
            return l2;
        }
        
        if(!l2){
            return l1;
        }
        
        ListNode *res = nullptr;
        ListNode **ptr;
        
        if(l1->val < l2->val){
            res = l1;
            l1 = l1->next;
        }else{
            res = l2;
            l2 = l2->next;
        }
        
        ptr = &res;
        
        //Merge 2 list
        while(l1 && l2){
            if(l1->val < l2->val){
                (*ptr)->next = l1;
                l1 = l1->next;
            }else{
                (*ptr)->next = l2;
                l2 = l2->next;
            }
            ptr = &((*ptr)->next);
        }
        
        if(l1){
            (*ptr)->next = l1;
        }else if(l2){
            (*ptr)->next = l2;
        }
                    
        return res;
    }
    
    ListNode *mergeK(vector<ListNode*>::iterator b, vector<ListNode*>::iterator e){
        if(b == e){
            return nullptr;
        }
        
        if(next(b) == e){
            return *b;
        }
        
        int mid = distance(b,e)/2;
        
        ListNode *left= mergeK(b,b + mid);
        ListNode *right = mergeK(b + mid,e);
        return merge(left,right);
    }
    
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        return mergeK(lists.begin(),lists.end());
    }
};
```

## 4.Performance

![](/files/-LHMVq-Y6k743fmtX-ud)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/23.-merge-k-sorted-lists-hard.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
