Merge k Sorted Lists 解答

Question

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

Solution 1 -- Merge Sort

We can follow the method of merge sort. Time complexity O(nlog(n)), n is the total number of all nodes.

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode mergeKLists(ListNode[] lists) {
11         if (lists == null || lists.length < 1)
12             return null;
13         if (lists.length == 1)
14             return lists[0];
15         int k = lists.length;
16         return MSort(lists, 0, k - 1);
17     }
18
19     private ListNode MSort(ListNode[] lists, int start, int end) {
20         if (start < end) {
21             int mid = (end - start) / 2 + start;
22             ListNode left = MSort(lists, start, mid);
23             ListNode right = MSort(lists, mid + 1, end);
24             return mergeTwoLists(left, right);
25         }
26         return lists[start];
27     }
28
29     private ListNode mergeTwoLists(ListNode a, ListNode b) {
30         ListNode newHead = new ListNode(0);
31         ListNode p = newHead, p1 = a, p2 = b;
32         while (p1 != null && p2 != null) {
33             if (p1.val < p2.val) {
34                 p.next = new ListNode(p1.val);
35                 p1 = p1.next;
36             } else {
37                 p.next = new ListNode(p2.val);
38                 p2 = p2.next;
39             }
40             p = p.next;
41         }
42         while (p1 != null) {
43             p.next = new ListNode(p1.val);
44             p1 = p1.next;
45             p = p.next;
46         }
47         while (p2 != null) {
48             p.next = new ListNode(p2.val);
49             p2 = p2.next;
50             p = p.next;
51         }
52         return newHead.next;
53     }
54 }

Solution 2 -- PriorityQueue

We can use PriorityQueue in Java. The elements of the priority queue are ordered according to their natural ordering, or by a comparator provided at the construction time.

PriorityQueue

Comparator

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode mergeKLists(ListNode[] lists) {
11         if (lists == null || lists.length < 1)
12             return null;
13         ListNode newHead = new ListNode(0);
14         PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(lists.length,
15                                             new Comparator<ListNode>() {
16                                                 public int compare(ListNode a, ListNode b) {
17                                                     return (a.val - b.val);
18                                                 }});
19         for (ListNode tmp : lists) {
20             if (tmp != null)
21                 pq.add(tmp);
22         }
23         ListNode p = newHead;
24         while (pq.size() > 0) {
25             ListNode tmp = pq.poll();
26             p.next = tmp;
27             if (tmp.next != null)
28                 pq.add(tmp.next);
29             p = p.next;
30         }
31         return newHead.next;
32     }
33 }
时间: 2024-12-20 01:26:10

Merge k Sorted Lists 解答的相关文章

LeetCode: 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. Show Tags 参考资料: http://blog.csdn.net/linhuanmars/article/details/19899259. SOLUTION 1: 使用分治法.左右分别递归调用Merge K sorted List,然后再使用merg

【leetcode】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. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

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. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

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

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组

[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 [022]

[题目] 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), 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