码迷,mamicode.com
首页 > 其他好文 > 详细

合并K个有序链表

时间:2019-08-03 00:14:18      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:back   lis   end   有序链表   优化   klist   while   tor   return   

合并 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。 

首先我想到的是两两合并,分析时间大概是接近O(n^2)了。leecode显示需要200ms.

/**
 * 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) {
        if(lists.size() == 0) return NULL;
        if(lists.size() == 1) {
            return lists[0];
        }  
        ListNode prehead = ListNode(-1);
        ListNode *pre = &prehead;            
        ListNode* p1 = lists[lists.size()-1];
        lists.pop_back();
        ListNode* p2 = mergeKLists(lists);
        while(p1 && p2) {
            if(p1->val < p2->val) {
                pre->next = p1;
                    //pre = pre->next;
                p1 = p1->next;
            } else {
                pre->next = p2;
                //pre = pre->next;
                p2 = p2->next;                    
            }
            pre = pre->next;
        }
        pre->next = p1?p1:p2;                
        return prehead.next;            
    }
             
};

后来借鉴了快速排序的思想。优化了一点时间,能够达到48ms。感觉还不是很满意。

/**
 * 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) {
        if(lists.size() == 0) return NULL;
        if(lists.size() == 1) {
            return lists[0];
        } 
        if(lists.size() == 2) {
            return merge2Lists(lists[0], lists[1]);
        }
        
        int mid = lists.size() /2;
        vector<ListNode*> left;
        vector<ListNode*> right;
        left.insert(left.begin(), lists.begin(), lists.begin()+mid);
        right.insert(right.begin(), lists.begin()+mid, lists.end());
        ListNode* leftNode = mergeKLists(left);
        ListNode* rightNode = mergeKLists(right);
        return merge2Lists(leftNode, rightNode);        
          
    }    
    ListNode* merge2Lists(ListNode*l1, ListNode*l2){
        ListNode prehead = ListNode(-1);
        ListNode *pre = &prehead;            
        //ListNode* p1 = lists[lists.size()-1];
        //lists.pop_back();
        //ListNode* p2 = mergeKLists(lists);
        ListNode *p1 = l1;
        ListNode *p2 = l2;
        while(p1 && p2) {
            if(p1->val < p2->val) {
                pre->next = p1;
                    //pre = pre->next;
                p1 = p1->next;
            } else {
                pre->next = p2;
                //pre = pre->next;
                p2 = p2->next;                    
            }
            pre = pre->next;
        }
        pre->next = p1?p1:p2;                
        return prehead.next;         
    }                 
};

 



合并K个有序链表

标签:back   lis   end   有序链表   优化   klist   while   tor   return   

原文地址:https://www.cnblogs.com/Shinered/p/11291647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!