LiteCode

Merge k Sorted Lists

HardLinked ListDivide and ConquerHeap (Priority Queue)
You are given an array of k linked lists, where each linked list is sorted in ascending order. Merge all of the linked lists into one sorted linked list and return it. Input format for LiteCode: Line 1: integer k, the number of lists Next k lines: each line begins with the list length, followed by that many sorted values Use 0 to represent an empty list Output format: Print the merged linked list as space-separated values. If the merged list is empty, print EMPTY.

Examples

Input: 3 3 1 4 5 3 1 3 4 2 2 6
Output: 1 1 2 3 4 4 5 6
Input: 0
Output: EMPTY
Input: 1 0
Output: EMPTY

Constraints

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i] is sorted in ascending order.
  • The sum of lists[i].length will not exceed 10^4.