Example #1
0
        public ListNode MergeKLists(ListNode[] lists)
        {
            var pq = new MinHeap();

            foreach (var node in lists)
            {
                if (node != null)
                {
                    pq.Add(node.val, node);
                }
            }

            ListNode head = null;
            ListNode curr = null;

            while (pq.map.Any())
            {
                var node = pq.PopMin();

                if (node.next != null)
                {
                    pq.Add(node.next.val, node.next);
                }

                if (curr == null)
                {
                    head = node;
                    head = curr;
                }
                else
                {
                    curr.next = node;
                    curr      = curr.next;
                }
            }

            return(head);
        }