23. Merge k Sorted Lists

标签: 链表
8人阅读 评论(0) 收藏 举报
分类:

题目描述

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

解题代码

 class Solution {
 public:
     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
         if (l1 == nullptr)
             return l2;
         if (l2 == nullptr)
             return l1;
         ListNode * head, *current;
         if (l1->val > l2->val) {
             head = new ListNode(l2->val);
             l2 = l2->next;
         }
         else {
             head = new ListNode(l1->val);
             l1 = l1->next;
         }
         current = head;
         while (l1 != nullptr && l2 != nullptr)
         {
             if (l1->val > l2->val) {
                 current->next = new ListNode(l2->val);
                 l2 = l2->next;
             }
             else {
                 current->next = new ListNode(l1->val);
                 l1 = l1->next;
             }
             current = current->next;
         }
         ListNode * temp = l1 == nullptr ? l2 : l1;
         while (temp != nullptr)
         {
             current->next = new ListNode(temp->val);
             temp = temp->next;
             current = current->next;
         }
         return head;
     }
     ListNode * mergeSubLists(vector<ListNode*> & lists, int start, int end) {
         if (start > end)
             return nullptr;
         if (start == end)
             return lists[start];
         int num = (start + end) / 2;
         ListNode * first = mergeSubLists(lists, start, num);
         ListNode * second = mergeSubLists(lists, num + 1,end);
         return mergeTwoLists(first, second);
     }

     ListNode* mergeKLists(vector<ListNode*>& lists) {
         if (lists.size() == 0)
             return nullptr;
         int num = lists.size() - 1;
         ListNode * first = mergeSubLists(lists, 0, num / 2);
         ListNode * second = mergeSubLists(lists, num / 2 + 1, num);
         return mergeTwoLists(first, second);
     }
 };

解题思路

  1. 将k个链表进行合并,每个链表使用一个指针,将k个元素进行比较得到最小值,这样子的时间复杂度是O(NK),N为元素个数,K为链表个数。为了减少比较次数,可以使用一个优先队列,复杂度为O(NlogK)(每次加一个新的元素到队列中,取出最小值,这个花费的时间是logK)。
  2. 使用归并排序的方法达到同样的复杂度。这也是上面代码使用的方法。不过上面的代码空间复杂度不好,mergeTwoLists中每次都创建了新的空间,并没有删除旧的空间。所以可以将传入的两个链表中的一个为主,另一个插入前一个并及时删除节点。(如果原始的链表不能删除,那可以在开始时复制一遍链表,或者在调用mergeTwoLists函数前判断是否是原始链表,是则不能合并后删除,但可能会出现一个链表是可以删除,一个是原始链表不可以删除的情况,在这种角度看的话使用1的优先队列好像更容易些)
查看评论

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译合并K个已排序的链表,并且将其排序并返回。 分析和描述其复杂性。原文Merge k sorted linked lists and return it as one sorted list. An...
  • NoMasp
  • NoMasp
  • 2015-11-12 17:30:10
  • 3550

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python)

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-03-05 11:19:11
  • 2630

leetcode_效率题解_23. Merge k Sorted Lists(合并k个有序链表)

相关题解: leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)题目链接 【题目】 Merge k sorted lin...
  • huangmx1995
  • huangmx1995
  • 2016-11-22 22:09:45
  • 352

【LeetCode】23. Merge k Sorted Lists 基于Java的解法

23. Merge k Sorted Lists Total Accepted: 88102 Total Submissions: 372066 Difficulty: Hard Merge k...
  • Jin_Kwok
  • Jin_Kwok
  • 2016-06-04 00:46:59
  • 1799

23. Merge k Sorted Lists 优先队列 比较器

把k个有序链表merge成一个有序的链表 Merge k sorted linked lists and return it as one sorted list. Analyze an...
  • u012985132
  • u012985132
  • 2016-11-17 22:35:23
  • 241

LeetCode(23)Merge K Sorted Lists

题目如下: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complex...
  • feliciafay
  • feliciafay
  • 2013-12-21 16:27:23
  • 4204

LeetCoder 23. Merge k Sorted Lists

题意合并nn个有序链表思路解法一: 将nn个链表依次和结果链表合并,直到合并完成解法二: 优先队列,即priority_queue,将链表中的数一次加入到优先队列中,因为默认的优先队列是按照从大到...
  • helloiamclh
  • helloiamclh
  • 2017-05-21 15:22:03
  • 233

LeetCode 23 Merge k Sorted Lists (C,C++,Java,Python)

Problem: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its c...
  • runningtortoises
  • runningtortoises
  • 2015-05-11 15:39:00
  • 1058

[Leetcode]23. Merge k Sorted Lists @python

题目Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity....
  • qian2729
  • qian2729
  • 2016-01-16 15:32:14
  • 659

LeetCode 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. ...
  • sunao2002002
  • sunao2002002
  • 2015-05-31 02:04:42
  • 3587
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 1万+
    积分: 1388
    排名: 3万+
    博客专栏
    最新评论