Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrit-ikkumar/2a9c12ecc6ae2d39517589d12af239c9 to your computer and use it in GitHub Desktop.
Save hrit-ikkumar/2a9c12ecc6ae2d39517589d12af239c9 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> a) {
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int i=0;i<a.size();i++) {
ListNode runner = a.get(i);
while(runner != null) {
pq.add(runner.val);
runner = runner.next;
}
}
ListNode head = new ListNode(0);
ListNode runner = head;
while(pq.size() != 0) {
runner.next = new ListNode(pq.poll());
runner = runner.next;
}
return head.next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment