链表两两合并,直至向量的size为1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    /*
    归并排序
    */
    ListNode* merge(ListNode* head1,ListNode* head2){ //两个有序链表合并
        ListNode *head=new ListNode(0),*p=head;
        while(head1&&head2){
            if(head1->val<head2->val){
                p->next=head1;
                p=p->next;
                head1=head1->next;
            }else{
                p->next=head2;
                p=p->next;
                head2=head2->next;
            }
        }
        while(head1){
            p->next=head1;
            p=p->next;
            head1=head1->next;
        }
        while(head2){
            p->next=head2;
            p=p->next;
            head2=head2->next;
        }
        return head->next;
   }
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        if(lists.size()==0) return NULL;
        while(lists.size()!=1){
            int size=lists.size(),i;
            for(i=0;i+2<=size;i+=2){ //合并两个链表
                lists.push_back(merge(lists[i],lists[i+1]));
            }
            lists.assign(lists.begin()+i,lists.end());//删除之前合并的链表
        }
        return lists[0];
    }
};