博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Merge k Sorted Lists
阅读量:6548 次
发布时间:2019-06-24

本文共 3739 字,大约阅读时间需要 12 分钟。

Solution and Precautions:

Very similar to the classical merger sort for two sorted arrays, since the k linked list are already sorted, we just find the smallest number among the heads list[i][0] (i=0,…k-1), of course, if some list already reached to the end, that is list[i][0] is NULL, we just ignore this list, the smallest number is obviously the smallest one in the combined or merged final list, we repeat this process and push the smallest number found into the tail of the final merger list until there is no elements leaft. To fast find the smallest number among the k head elements, we could adopt the heap data structure, thus each found and delete of the minHeap, it cost O(log k), and it is possible to perform n such operations where n is the total number of elements of the k list, thus the time complexity is bounded by O(n * log k).

Note, the bound could be even tighter since the size of the heap could be smaller when some list is consumed up. So for the delete min operations later on it might cost less than log k. (The exact bound is not easy to figure out for me)

 

Min Heap Method

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */ /* Min heap*/public class Solution {    public ListNode mergeKLists(ArrayList
lists) { if(lists == null || lists.size() == 0) return null; Comparator
comparator = new Comparator
(){ public int compare(ListNode m, ListNode n){ if(m.val == n. val) return 0; else if(m.val > n .val) return 1; else return -1; } }; PriorityQueue
p = new PriorityQueue
(lists.size(), comparator); for( int i = 0; i < lists.size(); i++){ if(lists.get(i) != null) p.add(lists.get(i)); } ListNode head = null, c =null; while(!p.isEmpty()){ if(head == null) { head = p.poll(); c = head; } else{ c.next = p.poll(); c = c.next; } if(c.next != null) p.add(c.next); } return head; } }

 

ref:http://blog.csdn.net/zyfo2/article/details/8682727

 

 

 

 

 

Use Merge Two sorted List Method

/* Merge Two lists method*/public class Solution {    public ListNode mergeKLists(ArrayList
lists) { ListNode head = null; int len = lists.size(); if(len == 0) return null; else if(len ==1) return lists.get(0); head = merge2List(lists.get(0), lists.get(1)); for(int i =2; i< len; i++){ head = merge2List(lists.get(i), head); } return head; } public ListNode merge2List(ListNode node1, ListNode node2){ ListNode head = new ListNode(Integer.MIN_VALUE); ListNode tmp = head; while(node1 != null && node2 != null){ if(node1.val <= node2.val){ ListNode node = new ListNode(node1.val); tmp.next = node; tmp = tmp.next; node1 = node1.next; }else{ ListNode node = new ListNode(node2.val); tmp.next = node; tmp = tmp.next; node2 = node2.next; } } if(node1 != null){ tmp.next = node1; } if(node2 != null){ tmp.next = node2; } return head.next; } }

 

转载于:https://www.cnblogs.com/RazerLu/p/3535608.html

你可能感兴趣的文章
足球——2011-2012意甲球队队标
查看>>
mysql性能优化
查看>>
网站出现安全证书过期的原因
查看>>
我的友情链接
查看>>
wordpress 登录实例(一)
查看>>
内网IT风险管控解决方案
查看>>
卡巴斯基端点安全10.0针对ESET5.x主要竞争优势
查看>>
Java基础学习总结(7)——Object类
查看>>
Vim编辑器的使用
查看>>
Data Guard Broker配置与主备库切换指南
查看>>
linux部署安装maven私有库
查看>>
手动安装K8s第九节:创建dashboard和发布应用
查看>>
Spring学习总结(4)——Spring AOP教程
查看>>
SpringMVC权限管理
查看>>
7月第一周的几个问题
查看>>
python实用小工具介绍
查看>>
CentOS 6.5 64 安装 mysql-5.7.19
查看>>
DNS基本原理
查看>>
iOS 中json解析数据出现中文乱码的问题
查看>>
spring工程在eclipse 运行报错:找不到ContextLoaderListener
查看>>