I have $k$ sorted arrays stored as linked lists. I need an algorithm to merge them into one single sorted array, also stored as a linked list. How can I do that?
My Code:
Function merge(A[k])
Begin:
// A is array of list to merge.
Array result;
temp //array to save element in each list
For (i=0:i<k:i++):
Temp.add(A[i].head.value);
While(result.size<n):
Minvalue, index = temp.pop()
Result.add(minvalue)
Value = 0
If( A[index].head.next==Null):
Value = maxInt
Else :
A[index].head = A[index].head.next
Value = A[index].head.key Temp.add(value)
Return result;
End