Created
June 14, 2022 18:29
-
-
Save hrit-ikkumar/2a9c12ecc6ae2d39517589d12af239c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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