/**
 * 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;
    }
};

 

相关文章:

  • 2021-09-01
  • 2021-11-03
  • 2022-12-23
  • 2021-08-19
  • 2021-04-22
猜你喜欢
  • 2022-01-20
  • 2021-10-19
  • 2021-06-03
  • 2021-12-26
  • 2022-02-07
  • 2021-05-31
相关资源
相似解决方案