# java – merging K linked ordered lists

Specifically, how can I improve the time complexity of my algorithm (currently it is `O (listLength * numberOfLists)`)? Only exceeds 5% of the solutions accepted from LeetCode, which surprised me.

``````/ **
* Definition for the list of individual links.
* public class ListNode {
* int val;
* Next ListNode;
* ListNode (int x) {val = x; }
*}
* /
class solution {
Empty private preview (Final ListNode[] listNodes, final int index) {
listNodes[index] = listNodes[index].following;
}

Public listNode mergeKLists (Final ListNode[] listNodes) {
ListNode sortedListNode = null;

int associatedIndex;

do {
int minValue = Integer.MAX_VALUE;
associatedIndex = -1;

for (int listIndex = 0; listIndex <listNodes.length; listIndex ++) {
End ListNode listNode = listNodes[listIndex];

if (listNode! = null && listNode.val <minValue) {
minValue = listNode.val;
associatedIndex = listIndex;
}
}

// An associated index of -1 indicates that there are no more values ​​left in any of the given lists
if (associatedIndex! = -1) {
if (sortedListNode == null) {
sortedListNode = new ListNode (minValue);
}
else {
sortedListNode.next = new ListNode (minValue);
sortedListNode = sortedListNode.next;
}

Keep in mind that the `Solution` class besides `ListNode` It is already provided, the only code that I wrote was inside. `merge lists`.