【Merge K Sorted Lists】cpp

题目:

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

代码:

/**
 * 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;
        return Solution::mergeK(lists, 0, lists.size()-1);
    }
    static ListNode* mergeK(vector<ListNode*>& lists, int begin, int end )
    {
            if ( begin==end ) return lists[begin];
            if ( (begin+1)==end ) return Solution::mergeTwo(lists[begin], lists[end]);
            int mid = ( begin + end ) / 2;
            ListNode *firstHalf = Solution::mergeK(lists, begin, mid);
            ListNode *secondHalf = Solution::mergeK(lists, mid+1, end);
            return Solution::mergeTwo(firstHalf, secondHalf);
    }
    static ListNode* mergeTwo( ListNode *h1, ListNode *h2 )
    {
            ListNode dummy(-1);
            ListNode *p = &dummy;
            while ( h1 && h2 ){
                if ( h1->val<h2->val ){
                    p->next = h1;
                    h1 = h1->next;
                }
                else{
                    p->next = h2;
                    h2 = h2->next;
                }
                p = p->next;
            }
            p->next = h1 ? h1 : h2;
            return dummy.next;
    }
};

tips:

多路归并写法。

时间: 2024-10-25 11:49:44

【Merge K Sorted Lists】cpp的相关文章

【Merge Two Sorted Lists】cpp

题目: 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. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode

leetcode 【 Merge Two Sorted Lists 】 python 实现

题目: 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. 代码:oj在线测试通过 Runtime: 208 ms 1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(

【Leetcode长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

【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】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

【LeetCode-面试算法经典-Java实现】【023-Merge k Sorted Lists(合并k个排好的的单链表)】

[023-Merge k Sorted Lists(合并k个排好的的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题目大意 合并k个排好的的单链表.分析和描述它的复杂性. 解题思路 使用小顶堆来实现,先将K个链表的头结点入堆,取堆顶元素,这个结点就是最小的,接

LeetCode: Merge k Sorted Lists [022]

[题目] 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), n

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个链表中有的