leetcode

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        const int len = lists.size();
        if(len==0) return NULL;
        ListNode *re =lists[0] ,*pre=NULL ,*temp=NULL;
        for(int i=1;i<lists.size();i++){
           ListNode *head = new ListNode (0);
           head->next = re;
           pre = head ;
            auto q=re;
            for(auto p=lists[i];p!=NULL;){
                while((q!=NULL&&p->val>q->val)){
                    pre=q;
                    q=q->next;
                }
                pre->next = p;
                temp= p->next;
                p->next = q;
                p=temp;
                pre = pre->next;
            }
            re=head->next;
            delete head;
        }
        return re;
    }
};
原文地址:https://www.cnblogs.com/NeilZhang/p/5513277.html