Merge k Sorted Lists

思路:可以直接使用merge two list中的函数,这里的时间复杂度O(n1+...),空间复杂度O(1),但运行中提示time limited exceeded,也可以直接将lists中数字存储在vector中,排序即可,这里时间复杂度O(Nlog(N)),空间复杂度(N);

/**
 * 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) {
        vector<int>nums;
        for(int i=0; i<lists.size(); ++i)
        {
            ListNode *cur = lists[i];
            for(; cur != nullptr; cur=cur->next)
                nums.push_back(cur->val);
        }
        
        sort(nums.begin(), nums.end());
        ListNode dummy(-1);
        ListNode *cur = &dummy;
        for(int i=0; i<nums.size(); ++i)
        {
            cur->next = new ListNode(nums[i]);
            cur = cur->next;
        }
        cur->next = nullptr;
        
        return dummy.next;
    }
};

思路二:使用归并排序的方式,这里需要注意处理list个数为奇数的情况,同时注意循环结束的条件,算法时间复杂度Nklog(k)

/**
 * 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) {
        int n = lists.size();
        if(n == 0)
            return nullptr;
            
        while(n > 1)
        {
            int k = (n+1) / 2;
            for(int i=0; i<n/2; ++i)
            {
                lists[i] = mergeTwoLists(lists[i], lists[i+k]);
            }
            n = k;
        }
        return lists[0];
    }
    
    ListNode* mergeTwoLists(ListNode *l1, ListNode *l2)
    {
        ListNode dummy(-1);
        ListNode *cur = &dummy;
        
        while(l1 != nullptr && l2 != nullptr)
        {
            if(l1->val < l2->val)
            {
                cur->next = new ListNode(l1->val);
                l1 = l1->next;
            }
            else
            {
                cur->next = new ListNode(l2->val);
                l2 = l2->next;
            }
            cur = cur->next;
        }
        
        cur->next = (l1 == nullptr) ? l2 : l1;
        
        return dummy.next;
    }
};

 思路三:使用STL中的优先级队列

priority_queue<class T, class Container=vector<T>, class Template=less<typename Contrainer::value_type>>

其中T是存储的数据类型,container是存储数据的结构,默认使用vector,cmp是比较结构,默认最大化队列

可以使用重载运算符<,这种情况下只能带一个模板参数:priority_queue<ListNode *> que;

bool operator<(ListNode *a, ListNode *b)

{

  return a->val > b->val;

}

更方便的可以使用仿函数:

struct cmp

{

  bool operator()(ListNode *a, ListNode *b)

  {

    return a->val > b->val;

  }

}

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    struct cmp{
        bool operator()(ListNode *a, ListNode *b)
        {
            return a->val > b->val;
        }
    };
    
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode dummy(-1);
        ListNode *cur = &dummy;
        
        priority_queue<int, vector<ListNode *>, cmp> que;
        
        for(int i=0; i<lists.size(); ++i)
            if(lists[i])
                que.push(lists[i]);
                
        while(!que.empty())
        {
            ListNode *p = que.top();
            que.pop();
            
            cur->next = p;
            cur = cur->next;
        
            if(p->next)
                que.push(p->next);
        }
        
        return dummy.next;
    }
};

 

posted @ 2017-03-31 09:35  chengcy  Views(112)  Comments(0)    收藏  举报