【LeetCode】【数组归并】Merge k Sorted Lists

描述

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

Example:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6

思路

借鉴Merge Two Sorted Lists的解决思路,对两个数组先进行排序,然后两两组合,排成一个序列。

将此视为一种迭代的分而治之的解决方案。

mergeTwoLists方法是一种递归的合并两个序列的方法

注意:

尽量不用erase方法,太耗时。

一些优化以避免vector.erase()

比如下述:

 while(lists.size() > 1){
        lists.push_back(mergeTwoLists(lists[0], lists[1]));
        lists.erase(lists.begin());
        lists.erase(lists.begin());
    }
    return lists.front();

改为使用指针方法遍历vector,最后取vector.back()

最终解决方案:

/**
 * 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.empty()){
            return nullptr;
        }
        if(lists.size() == 1) return lists[0];
        int i = 0;
        while(i < lists.size() - 1){
            lists.push_back(mergeTwoLists(lists[i], lists[i+1]));
            i += 2;
        }
        return lists.back();
    }

    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == nullptr){
            return l2;
        }
        if(l2 == nullptr){
            return l1;
        }
        if(l1->val <= l2->val){
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        }
        else{
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};

原文地址:https://www.cnblogs.com/ygh1229/p/9718627.html

时间: 2024-10-13 20:50:42

【LeetCode】【数组归并】Merge k Sorted Lists的相关文章

LeetCode OJ:Merge k Sorted Lists(归并k个链表)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类似于归并2个链表,暴力一点的方法就是,每取出一个list就与以前的list归并返回merge后list,知道所有list merge完成. 但是可惜,这样做会TLE.贴下代码先: 1 /** 2 * Definition for singly-linked list. 3 * struct List

LeetCode 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 本题在上一题(LeetCode 21: Merge Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个. 此题有如下几种解法: 1.最简单的方法莫过于每次都从K个链表头中找到最小的那个元素,加入结果链表中.基于此,有人通过最小堆来简化最小元素的比较. struct Compa

[Leetcode][Python]23: Merge k Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 23: Merge k Sorted Listshttps://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. ===Comments by

【一天一道LeetCode】#23. Merge k Sorted Lists

一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. (二)解题 合并K个已拍好序的链表.剑指上有合并两个已排好序的链表的算法,那么K个数,我们可以采用归并排序的思想,不过合并函数可能需要修改一下,换成合并两个已排好序的链表的方法.代码如下: /** * Definition for singly-linked

【LeetCode】23. Merge k Sorted Lists

合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeKLists(vect

Python 解LeetCode:23. Merge k Sorted Lists

题目描述:把k个排序的链表组成的列表合并成一个排序的链表 思路: 使用堆排序,遍历列表,把每个列表中链表的头指针的值和头指针本身作为一个元素放在堆中: 第一步中遍历完列表后,此时堆中最多会有n个元素,n是列表的长度: 当堆不为空,取出堆中的最小值,然后把该值的指针指向下一个元素,并入堆: 第3步可以确保堆永远是o(n)大小的: 堆为空返回头结点就可以了 # Definition for singly-linked list. # class ListNode(object): # def __i

LeetCode题解之 Merge k Sorted Lists

1.题目描述 2.问题分析 使用合并两个链表的方法,逐次合并,效率较低.可以考虑同时合并K个链表. 3.代码 1 ListNode* mergeKLists(vector<ListNode*>& lists) { 2 3 ListNode *res = NULL; 4 for (vector<ListNode*>::iterator it = lists.begin(); it != lists.end(); it++) { 5 res = mergeTwoLists(re

leetcode problem :23. Merge k Sorted Lists

class Solution { public: ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode* head = NULL; ListNode* current = NULL; for (int i = 0; i < lists.size(); ++i){ if (!lists[i]){ lists.erase(lists.begin()+i); --i; } } while (lists.size()){ i

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

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