Personal Website and Computer Science TUR EN

Interview Question Merge k Sorted Lists

Last update: 1/8/2017 10:25:00 AM


The problem is to merge k-sorted lists and return it as a new one. We can take an inspiration the problem Merge Two Sorted Lists. There was only two lists in that problem. Now, we have been facing to merge multiple lists. However, we can still use the same method that we defined in there.

Problem #23 - Merge k Sorted Lists

Description: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity..

Difficulty: Hard

Solution in Java

package com.fatihkabakci.Hard.MergekSortedLists;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

 * @author fkabakci
 * Problem Description: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
 * Solution:
 * The probiem is about merging k sorted lists. Take an inspiration the problem based on 2 sorted lists.
 * 0. Use the method mergeTwoLists() which was coded in 2-sorted lists problem.
 * 1. Move all elements of the array into a list.
 * 2. Get two nodes of front and merge them. Then add them end of the list until the list has only one node.

public class MergekSortedLists {
	public ListNode mergeKLists(ListNode[] lists) {
		List<ListNode> l = Arrays.asList(lists);
		LinkedList<ListNode> o = new LinkedList<ListNode>(l);
		while (o.size() > 1) {
			ListNode head = o.poll();
			ListNode tail = o.poll();
			o.add(mergeTwoLists(head, tail));
		return o.poll();

	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
		ListNode head = new ListNode(0);
		ListNode l3 = head;
		while (l1 != null || l2 != null) {
			int val1 = (l1 != null) ? l1.val : Integer.MAX_VALUE;
			int val2 = (l2 != null) ? l2.val : Integer.MAX_VALUE;
			l3.next = new ListNode(Math.min(val1, val2));
			l3 = l3.next;
			if (val1 < val2)
				l1 = l1.next;
				l2 = l2.next;
		return head.next;

	public static void main(String[] args) {
		MergekSortedLists r = new MergekSortedLists();
		ListNode head = Data.init(new int[] { 1, 2, 3 });
		ListNode tail = Data.init(new int[] { 4, 5, 6 });
		ListNode[] list = { head, tail };
		ListNode entire = r.mergeKLists(list);
		while (entire != null) {
			System.out.print(entire.val + " ->");
			entire = entire.next;

Similar Problems

Merge Two Sorted Lists

ListNode and the helper class Data which is used to initialize linked list are below.

public class ListNode {
	int val;
	ListNode next;
	ListNode(int x) {
		val = x;

package com.fatihkabakci.Easy.RemoveNthNodeFromEndofList;

public class Data {
	public static ListNode init(int[] data) {
		ListNode head = new ListNode(data[0]);
		ListNode t = head;
		for(int i = 1; i < data.length; i++) {
			t.next = new ListNode(data[i]);
			t = t.next;
		return head;

1/7/2017 11:26:00 PM


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.