FastPrepMerge K Sorted Linked Lists
Problems/Merge K Sorted Linked Lists

Merge K Sorted Linked Lists

The Walt Disney Company logoThe Walt Disney Company● HardFULLTIMEOAONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The collected historical list names Merge N Sorted List, and another report names merging multiple sorted lists.

The reports omit the representation and bounds. Here, at most ten native, disjoint, sorted linked lists have up to fifty nodes each; relink those nodes, retain duplicate values and allow empty lists.

The judged task matches the reported merge capability at about 80%.

Merge all the sorted singly linked lists into one nondecreasing linked list. Return its head, retaining every original node exactly once, including duplicate values.

Relink existing nodes without changing their values. You may update the input head array and use a dummy node that is not returned. Inputs and outputs are serialized as value arrays; an empty array represents an empty linked list. The outer input array contains the individual lists.

Function

mergeKLists(lists: ListNode[]) → ListNode

Examples

Example 1

lists = [[0,4],[1,3],[2,5]]return = [0,1,2,3,4,5]

Interleaving the three sorted chains gives [0,1,2,3,4,5]; all six original nodes remain in the result.

Example 2

lists = [[],[-2,7],[]]return = [-2,7]

Only the middle list has nodes, so the result is [-2,7].

Constraints

  • 0 <= lists.length <= 10
  • 0 <= lists[i].length <= 50
  • -1000000 <= lists[i][j] <= 1000000
  • Every input list is sorted in nondecreasing order, acyclic, and disjoint from the other input lists.

More The Walt Disney Company problems

  • Find All Paths for a Name in a File SystemPHONE SCREEN · Seen Nov 2026
  • Resizable Array-Backed Integer SetPHONE SCREEN · Seen Sep 2026
  • Shortest Path with Directional Move CostsPHONE SCREEN · Seen Sep 2026
All The Walt Disney Company interview questionsSee The Walt Disney Company hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        // Write your code here
    }
}
Checking account…