Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public ListNode mergeKLists(ListNode[] lists) {
- Comparator<ListNode> com = new Comparator<ListNode>() {
- public int compare(ListNode l1, ListNode l2) {
- return l1.val - l2.val;
- }
- };
- PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(1, com);
- ListNode dummy = new ListNode(0);
- ListNode cur = dummy;
- for (ListNode li: lists) {
- if (li == null) {
- continue;
- }
- pq.offer(li);
- }
- while (!pq.isEmpty()) {
- ListNode min = pq.poll();
- cur.next = min;
- cur = cur.next;
- if (min.next != null) {
- pq.offer(min.next);
- }
- }
- cur.next = null;
- return dummy.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement