29 lines
728 B
C#
29 lines
728 B
C#
public class Solution
|
|
{
|
|
public ListNode? MergeKLists(ListNode[] lists)
|
|
{
|
|
ListNode? head = null;
|
|
ListNode? tail = null;
|
|
var queue = new PriorityQueue<ListNode, int>();
|
|
foreach (var l in lists)
|
|
if (l != null)
|
|
queue.Enqueue(l, l.val);
|
|
while (queue.Count > 0)
|
|
{
|
|
var l = queue.Dequeue();
|
|
if (head == null || tail == null)
|
|
{
|
|
head = l;
|
|
tail = l;
|
|
}
|
|
else
|
|
{
|
|
tail.next = l;
|
|
tail = l;
|
|
}
|
|
if (l.next != null)
|
|
queue.Enqueue(l.next, l.next.val);
|
|
}
|
|
return head;
|
|
}
|
|
} |