leecode 题解 || Merge k Sorted Lists 问题

problem:

Merge k sorted linked lists and return it as one sorted list.
 Analyze and describe its complexity.

Tags Divide and Conquer Linked List Heap

合并K个已序单链表

thinking:

(1)题目没有要求不能够新开ListNode,所以暴力破解法:提取K个list的keyword。排序、新建结点插入。这样的情况对原list是否排好序没有要求。

排序时间复杂度能够做到O(N* log N )。提取keyword和新建结点的时间复杂度都为O(N),所以总的时间复杂度为O(N*logN),这没有考虑新建结点的时间浪费和空间           浪费。

(2)採用能够容纳结点的容器,想到的是堆,或者堆的封装-优先队列,因为堆的O(N*logN)排序的时间复杂度。并且不用新开结点,节省空间。

暴力法:

/**
 * 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) {
        ListNode *newlist=NULL;
        vector<int> tem;
        for(int i=0;i<lists.size();i++)
        {
            while(lists.at(i)!=NULL)
            {
                tem.push_back(lists.at(i)->val);
                lists.at(i)=lists.at(i)->next;
            }
        }
        if(tem.size()==0)
            return NULL;
        sort(tem.begin(),tem.end());
        for(int i=tem.size()-1;i>=0;i--)
        {
            ListNode *p = new ListNode(tem.at(i));
            p->next = newlist;
            newlist = p;

        }
        return newlist;
    }//mergeKLists()

};

优先队列法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class ListNodeCompare:public binary_function<ListNode*,ListNode*,bool>
{
public:
    bool operator()(ListNode* t1,ListNode* t2)const
    {
        if ( !t1||!t2 )
            return !t2;
        return t1->val>t2->val;
    }
};
class Solution {
public:
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if (lists.empty())
            return NULL;
        priority_queue<ListNode*,vector<ListNode*>,ListNodeCompare> Q;
        for(int i=0;i<lists.size();i++)
            if ( lists[i]!=NULL)
                Q.push(lists[i]);
        ListNode guard(-1);
        ListNode* tail=&guard;
        while(!Q.empty())
        {
            ListNode* toAdd=Q.top();
            Q.pop();
            tail->next=toAdd;
            tail=tail->next;
            if (toAdd->next)
                Q.push(toAdd->next);
        }
        return guard.next;
    }
};
时间: 2024-11-05 15:53:28

leecode 题解 || Merge k Sorted Lists 问题的相关文章

[LeetCode 题解]: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

Merge k Sorted Lists leetcode java

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解: Merge k sorted linked list就是merge 2 sorted linked list的变形题. 而且我们很自然的就想到了经典的Merge Sort,只不过那个是对数组进行sort.而不同的地方,仅仅是Merge两个list的操作不同. 这里来复习一下Merge

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组