Merge K Sorted Lists

HardLinked List
Next
Linked ListHeapDivide and Conquer

You are given k sorted linked lists. Merge them into one sorted list and print its values. The values are given as an array; build the linked list from them yourself, then solve it with pointers.

Input

Line 1: k, the number of lists. Next k lines: each starts with the list's length len, followed by its len values in non-decreasing order.

Output

Print the merged list's values on one line.

Constraints

1 <= k <= 100 The total number of nodes is between 1 and 10^4. -10^4 <= value <= 10^4

Example 1
Input
3
3 1 4 5
3 1 3 4
2 2 6
Output
1 1 2 3 4 4 5 6

Merging all three gives 1 1 2 3 4 4 5 6.

Hints

Merge K Sorted Lists - Hard Linked List Problem | Skill Shiksha