合并K个有序链表

Posted shinered

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了合并K个有序链表相关的知识,希望对你有一定的参考价值。

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

首先我想到的是两两合并,分析时间大概是接近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个有序链表的主要内容,如果未能解决你的问题,请参考以下文章

小C秋招面试算法题:合并k个有序数组合并k个有序链表(分治思想)

小C秋招面试算法题:合并k个有序数组合并k个有序链表(分治思想)

合并k个有序链表

经典算法——合并K个有序链表

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

合并K个有序链表