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

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode mergeKLists(List<ListNode> lists) {
14         ListNode head=new ListNode(-1);
15         ListNode tail=head;
16         if(lists.size()<=0) return null;
17         Comparator<ListNode> mycmp=new Comparator<ListNode>()
18         {
19             public int compare(ListNode l1,ListNode l2)
20             {
21                 if(l1.val>l2.val) return 1;
22                 else if(l1.val<l2.val)return -1;
23                 return 0;
24
25
26
27             }
28
29         } ;
30
31         PriorityQueue<ListNode> prq=new PriorityQueue<ListNode>(lists.size(),mycmp);
32         for(ListNode n:lists)
33         {
34             if(n!=null)
35             {
36                 prq.add(n);
37             }
38
39         }
40         while(prq.size()>0)
41         {
42             ListNode tem=prq.poll();
43             tail.next=tem;
44             tail=tail.next;
45             if(tem.next!=null)
46             {
47                 prq.add(tem.next);
48             }
49
50
51         }
52
53             return head.next;
54
55
56
57
58
59     }
60 }

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

时间: 2024-10-29 11:50:02

(java) Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.的相关文章

[LeetCode][Java] Merge k Sorted Lists

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意: 合并 K 个有序的链表,把他们合并成为一个有序链表.分析并描述它的复杂度. 算法分析: 先将k个链表转化为数组,合并,之后利用Collections.sort()排序. 将排序后的数组重新转化为链表,返回链表的头结点. AC代码: /** * Definition for singly

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

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 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 complexity. Solution: 采用胜者树的方法,胜者树介绍:胜者树,假如链表数组长度为n,链表元素总个数为k,那么时间复杂度为O(k*log(n)) 题目大意: 给定一个数组有序链表(数组元素可能为空),要求将这些链表合并为一个有序的链表. Java源代码(522ms): /** * Defi

23. Merge K Sorted Lists (Java, 归并排序的思路)

题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解析:合并k个已经有序的单链表,使其最终成为一个有序的单链表.原理就是归并排序,递归运算.基本算法recusion 与 merge 编码: public ListNode mergeKLists(ListNode[] lists) { if(lists == null || lists.leng

Java [leetcode 23]Merge k Sorted Lists

题目描述: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 分而自治方法.将K个List不断地分解为前半部分和后半部分.分别进行两个List的合并.最后将合并的结果合并起来. 代码如下: /** * Definition for singly-linked list. * public class ListNode { * int va

23. Merge k Sorted Lists (JAVA)

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 /** * Definition for singly-linked list.

LeetCode #23 Merge k Sorted Lists (H)

[Problem] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [Analysis] 这题一上来,就会想到之前做过的Merge Two Sorted Lists.但是如果单纯地不断将新的list merge进结果,复杂度是非常高的,因为仔细想想就会发现在这个过程中结果里已经merge过的元素需要被重新排序,而这一部分无用功是可以通过使用合理的技

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 * Def