[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 list.
 2 # class ListNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.next = None
 6
 7 class Solution(object):
 8     def mergeTwolists(self,l1,l2):
 9         """
10         :type l1: ListNode
11         :type l2: ListNode
12         :rtype: ListNode
13         """
14         ans = ListNode(0)
15         tmp = ans
16         if l1 == None and l2 == None:
17             return None
18         while l1 !=None or l2 != None:
19             if l1 == None:
20                 while l2 != None:
21                     tmp.val = l2.val
22                     l2 = l2.next
23                     if l2 == None:
24                         break
25                     tmp.next = ListNode(0)
26                     tmp = tmp.next
27                 break
28             if l2 == None:
29                 while l1 != None:
30                     tmp.val = l1.val
31                     l1 = l1.next
32                     if l1 == None:
33                         break
34                     tmp.next = ListNode(0)
35                     tmp = tmp.next
36                 break
37             if l1.val <= l2.val:
38                 tmp.val = l1.val
39                 l1 = l1.next
40             else:
41                 tmp.val = l2.val
42                 l2 = l2.next
43             tmp.next = ListNode(0)
44             tmp = tmp.next
45         return ans
46     def mergeKLists(self, lists):
47         """
48         :type lists: List[ListNode]
49         :rtype: ListNode
50         """
51         size = len(lists)
52         if size == 0:
53             return None
54         if size == 1:
55             return lists[0]
56         n = size // 2
57         tmp1 = self.mergeKLists(lists[:n])
58         tmp2 = self.mergeKLists(lists[n:])
59         return self.mergeTwolists(tmp1,tmp2)
60         



转载请注明出处:http://www.cnblogs.com/chruny/p/4872905.html

时间: 2024-10-23 20:08:12

[LeetCode]题解(python):023-Merge k Sorted Lists的相关文章

[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 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][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 023 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. 分析: 参考网址:http://blog.csdn.net/a83610312/article/details/8554241 思路是:首先将k个链表的第一个节点集合,建堆,然后取出堆顶的节点,链接到结果链表上,然后将该节点的下一个节点入堆,直到所有

Java for LeetCode 023 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路一: 之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大.如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到

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). /** * De

023. Merge k Sorted Lists

方法一:时间复杂度O(m1 + m2 + .. m2),超时 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(vector<L

[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 题解]: 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