[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 Dabay===

递归:每次把lists折半,分别处理左右两个子lists,然后merge。和归并排序的算法一样。时间复杂度为NlgN‘‘‘

# Definition for singly-linked list.class ListNode:    def __init__(self, x):        self.val = x        self.next = None

class Solution:    # @param a list of ListNode    # @return a ListNode    def mergeKLists(self, lists):        def merge(node1, node2):            head = node = ListNode(0)            while node1 and node2:                if node1.val < node2.val:                    node.next = node1                    node1 = node1.next                else:                    node.next = node2                    node2 = node2.next                node = node.next            if node1:                node.next = node1            elif node2:                node.next = node2            return head.next

        if len(lists) == 0:            return None        if len(lists) == 1:            return lists[0]        if len(lists) == 2:            return merge(lists[0], lists[1])

        node1 = self.mergeKLists(lists[:len(lists)/2])        node2 = self.mergeKLists(lists[len(lists)/2:])        return merge(node1, node2)

def main():    s = Solution()    ln1 = ListNode(1)    ln1.next = ListNode(10)    ln2 = ListNode(2)    ln2.next = ListNode(20)    node = s.mergeKLists([ln1, ln2])    while node:        print "%s->" % node.val,        node = node.next    print "None"

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-11-04 08:25:04

[Leetcode][Python]23: Merge k Sorted Lists的相关文章

【一天一道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

Python 解LeetCode:23. Merge k Sorted Lists

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

【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

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

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =

#23 Merge k Sorted Lists (N路归并排序)

#23 Merge k Sorted Lists (N路归并排序) 题目地址:#23 题目分类:链表/归并排序/堆排序 题目难度:hard 题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 翻译:合并K个已经排序的链表,返回一个排序好的链表. 思路 暴力法,我们很容易想到:以一个for循环遍历所有的链表,找出最小的,然后将这个节点加入新的链表

23. Merge k Sorted Lists(js)

23. 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题意:按大小顺序合并n条链表代码

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link