LeetCode: MergekSortedLists

Title:

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

排好序的,然后merge,很容易让人联想到归并排序。可以将k个序列按照二分进行分割,然后当长度为1时返回一个排好序的序列,最后将两个序列merge

class Solution{
public:
    ListNode* merge(ListNode* list1, ListNode* list2){
        ListNode head(0),*tail = &head;
        while (list1 != NULL && list2 != NULL){
            if (list1->val < list2->val){
                tail->next = list1;
                tail = list1;
                list1 = list1->next;
            }else{
                tail->next = list2;
                tail = list2;
                list2 = list2->next;
            }
        }
        if (list1 != NULL){
            tail->next = list1;
        }
        if (list2 != NULL){
            tail->next = list2;
        }
        return head.next;
    }
    ListNode* divide(int l,int r,vector<ListNode* > &lists){
        int m = (l + r) / 2;
        if (l < r){
            return merge(divide(l,m,lists),divide(m+1,r,lists));
        }else
            return lists[l];
    }
    ListNode* mergeKLists(vector<ListNode* > &lists){
        int k = lists.size();
        if (0 == k)
            return NULL;
        return divide(0,k-1,lists);
    }
};
时间: 2024-11-05 11:37:31

LeetCode: MergekSortedLists的相关文章

[LeetCode]题解(python):023-Merge k Sorted Lists

题目来源: https://leetcode.com/problems/merge-k-sorted-lists/ 题意分析: 给定k个有序的链表,将这些链表整合成一个新的有序链表. 题目思路: 前面我们已经给出了两个有序链表整合的做法.这里,我们不妨用归并排序的想法,把n个链表看成 n/2 和n - n/2的整合,直到n/2 <= 1.时间复杂度是 O(n * (2^log k)) = O(n * k). 代码(python): 1 # Definition for singly-linked

LeetCode Merge k Sorted Lists 解决报告

https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走路List<ListNode>.对当中每一个链表同当前链表做一遍类似于归并排序最后一步的merge操作. 算法复杂度是O(KN) public class Solution { ListNode mergeTwoLists(ListNode list1, ListNode list2) { Lis

[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]Merge k Sorted Lists @ Python [基础知识: heap]

原题地址: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个已经排好序的链表, 使用堆这一数据结构. 堆,也叫做:priority queue 首先将每条链表的头节点进入堆中. 然后将最小的弹出,并将最小的节点这条链

[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题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

leetcode 链表题总结

按照frequency来排序,总共27题 1.2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/#/description 两个链表相加,注意就是进位问题. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *

[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 23. 合并K个排序链表(Merge Two Sorted Lists)

23. 合并K个排序链表 23. Merge k Sorted Lists 题目描述 合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. LeetCode23. Merge k Sorted Lists困难 示例: 输入: [ ??1->4->5, ??1->3->4, ??2->6] 输出: 1->1->2->3->4->4->5->6 Java 实现 public class ListNode { int va