# 23. Merge k Sorted Lists

## 1.問題&#x20;

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

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LHMTeM4h9gUsID5V539%2F-LHMVSy0PUMpSDZN7N4k%2F2018071403.jpg?alt=media\&token=eef053d2-ce99-4804-a9a4-5621d560437f)

## 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

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LHMTeM4h9gUsID5V539%2F-LHMVq-Y6k743fmtX-ud%2F2018071404.jpg?alt=media\&token=c248410c-3517-4676-b426-fdcf7c5a15f4)


---

# 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://jenhsuan.gitbook.io/algorithm/leetcode/23.-merge-k-sorted-lists-hard.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.
