leetcode第22题--Merge k Sorted Lists

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

先合并两个list,再根据归并排序的方法递归合并。假设总共有k个list,每个list的最大长度是n,那么运行时间满足递推式T(k) = 2T(k/2)+O(n*k)。根据主定理,可以算出算法的总复杂度是O(nklogk)。空间复杂度的话是递归栈的大小O(logk)。



/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) // 将两个list  merge
{
    if(!l1)
        return l2;
    if(!l2)
        return l1;
    ListNode *tmp = new ListNode(0);
    ListNode *ans = tmp;
    while(l1 && l2)
    {
        if(l1 -> val < l2 -> val)
        {
            ans -> next = l1;
            ans = ans -> next;
            l1 = l1 -> next;
        }
        else
        {
            ans -> next = l2;
            ans = ans -> next;
            l2 = l2 -> next;
        }
    }
    if (l1)
    {
        while(l1)
        {
            ans -> next = l1;
            ans = ans -> next;
            l1 = l1 -> next;
        }
    }

    if (l2)
    {
        while(l2)
        {
            ans -> next = l2;
            ans = ans -> next;
            l2 = l2 -> next;
        }
    }
    ans = tmp;
    delete ans;
    return tmp -> next;
}
ListNode *reMerge(vector<ListNode *> &lists, int l, int r)
{
    if(l < r) // 类似于归并排序,把一大块分成l和r两块,然后将两个合并(l和r也还可以再分)
    {
        int m = (l + r)/2;
        return mergeTwoLists(reMerge(lists, l, m), reMerge(lists, m + 1, r));
    }
    else
        return lists[l];
}
ListNode *mergeKLists(vector<ListNode *> &lists)
{
    ListNode *ans = NULL;// 用NULL 而不是利用 new ListNode(0)
    if (lists.size() == 0)
        return ans;
    return reMerge(lists, 0, lists.size() - 1);
}
};

 
时间: 2024-11-05 09:05:41

leetcode第22题--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. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

leetcode第21题-Merge Two Sorted Lists

本题目是意思是把两个有序的链表合成一个有序的链表,考察了归并算法和链表的操作. 代码也相对比较简单,简单说一下归并函数里三个指针的作用,sum是返回的第一个指针,cur是所要返回的链表里走到的位置,put是对于取到的l1或l2里的某一个指针节点.全部的可运行代码如下: #include<stdio.h> #include<string.h> #include<stdlib.h> struct ListNode{ int value; ListNode *next; };

【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 刷题之路 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个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组

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 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]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

LeetCode 21 23:Merge Two Sorted Lists &amp; Merge K Sorted Lists

LeetCode 21: 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. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

[LeetCode][JavaScript]Merge k Sorted Lists

https://leetcode.com/submissions/detail/28015840/ Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 直接使用了“Merge Two Sorted Lists”里写的方法. http://www.cnblogs.com/Liok3187/p/4514347.ht