LeetCode23-合并k个有序链表

今天白天没课

自己还是早早的就过来了

上午刷了一道题之后

还是一如既往的看了一场我詹的比赛

下午上党课

晚上在实验室做应用数理统计的题目时

突然在想

自己每天都在干嘛

每天都泡在实验室都干了些什么

觉得自己忙活了一天

但收获太少

深深地恐惧

有人说:当你有这种感觉的时候说明你还年轻

我亦不清楚

但我知道的是

明天早上起来吃一个梅干菜和鱼香肉丝的包子

对我来说就又是新的一天啦


23-合并k个有序链表

合并 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

示例:

输入:
[
  1->4->5,
  1->3->4,
  2->6
]
输出: 1->1->2->3->4->4->5->6

这一题与前面求合并2个有序链表极其类似,因此我当时做这题的时候第一想法就是借鉴前面求合并2个有序链表的方法,即不停的遍历2个链表,最终肯定能得到结果的,但是执行效率太低了,基本上是5000ms以上了。

代码如下:

class Solution:
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        l_new = ListNode(0)
        if len(lists) == 0:
            return []
        if len(lists) == 1:
            return lists[0]
        l_new.next = self.mergeTwoLists(lists[0], lists[1])
        if len(lists) == 2:
            return l_new.next
        for index in range(2, len(lists)):
            l_new.next = self.mergeTwoLists(l_new.next, lists[index])
        return l_new.next

    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        l1_copy = l1
        l2_copy = l2
        combination = ListNode(0)
        com_copy = combination
        while l1_copy and l2_copy:
            if l1_copy.val <= l2_copy.val:
                com_copy.next = l1_copy
                l1_copy = l1_copy.next
                com_copy = com_copy.next
            else:
                com_copy.next = l2_copy
                l2_copy = l2_copy.next
                com_copy = com_copy.next
        if l1_copy and not l2_copy:
            com_copy.next = l1_copy
        elif not l1_copy and l2_copy:
            com_copy.next = l2_copy
        return combination.next


if __name__ == "__main__":
    l1 = ListNode(0)
    l1_copy = l1
    l2 = ListNode(0)
    l2_copy = l2
    l1_num = [1, 2, 4]
    l2_num = [3, 6, 9]
    for num in l1_num:
        l1_copy.next = ListNode(num)
        l1_copy = l1_copy.next
    for num in l2_num:
        l2_copy.next = ListNode(num)
        l2_copy = l2_copy.next
    lists = [l1.next, l2.next]
    result = Solution().mergeKLists(lists)
    while result:
        print(result.val)
        result = result.next

执行效率如下:

 

看到这个执行时间之后,说实话,特别受挫,本来觉得特简单的一题却整出了个幺蛾子,于是我痛定思痛又整出了个执行效率有了极大提高的算法(其实是借鉴了网上大佬的想法)。这种思想其实我在求合并2个有序链表时已经提到啦,当时好像是没给出具体代码,其实是为了这儿买个伏笔,不然就没新鲜感了嘛!

具体思想:尤其是对于多个有序链表来说这种思想尤为适用,可以新建一List数组,用于存放所有链表对应节点的值,然后直接用sort()函数可以直接将数组中的元素按从小到大的顺序排列,这样可极大地节省链表排序的时间(这儿只想说:这方法太牛逼啦),最后只需新建一链表把数组中的值依次赋值给该链表即可。

代码如下:

class Solution:
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        if len(lists) == 0:
            return []
        L = ListNode(0)
        L_copy = L
        val_lists = []
        for list_index in range(len(lists)):
            single_list = lists[list_index]
            while single_list:
                val_lists.append(single_list.val)
                single_list = single_list.next
        val_lists.sort()
        for index in val_lists:
            L_copy.next = ListNode(index)
            L_copy = L_copy.next
        return L.next


if __name__ == "__main__":
    l1 = ListNode(0)
    l1_copy = l1
    l2 = ListNode(0)
    l2_copy = l2
    l1_num = [1, 2, 4]
    l2_num = [3, 6, 9]
    for num in l1_num:
        l1_copy.next = ListNode(num)
        l1_copy = l1_copy.next
    for num in l2_num:
        l2_copy.next = ListNode(num)
        l2_copy = l2_copy.next
    lists = [l1.next, l2.next]
    result = Solution().mergeKLists(lists)
    while result:
        print(result.val)
        result = result.next

执行效率那是没话说,基本上都在90%以上,很欣慰了。

 

猜你喜欢

转载自blog.csdn.net/weixin_36431280/article/details/84397253
今日推荐