# Merged Sort Approach

• We can take merged sort approach. This will have complexity of O(K* LogN) where K is number of elements total in N list.

``````public class MergeKLinkList {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println(inputList);

System.out.println(result);
in.close();
}

public static ArrayList<SingleLinkedList<Integer>> GetInput(Scanner in)
{
int numberOfLists = in.nextInt();
int count = 0;

while(count < numberOfLists)
{
int number = in.nextInt();
int innerCount = 0;
while(innerCount < number)
{
innerCount++;
}

count++;
}

return inputList;
}

{
if (startIndex > endIndex)
{
return null;
}

if (startIndex == endIndex)
{
}

if (startIndex + 1 == endIndex)
{
}

int midIndex = startIndex + (endIndex - startIndex) / 2;
SingleLinkNode<Integer> B = MergeKSortedLinkedList(list, midIndex + 1, endIndex);

return MergeTwoList(A, B);
}

{
if (A == null)
{
return B;
}

if (B == null)
{
return A;
}

if ( A.data > B.data)
{
result = new SingleLinkNode<Integer>(B.data, null);
result.next =  MergeTwoList(A, B.next);
}
else
{
result = new SingleLinkNode<Integer>(A.data, null);
result.next = MergeTwoList(A.next, B);
}

return result;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.