Skip to main content
Back to problems
Leetcode
Medium
Linked Lists
Heaps
Divide and Conquer
Amazon
Google
Merge K Sorted Lists

Merge multiple sorted singly linked lists into one sorted linked list.

Acceptance 100%
Problem Statement

You are given an array of kk singly linked lists, where each list is already sorted in non-decreasing order.

Merge all of the lists into one sorted linked list and return the head of the merged list.

Your solution should reuse existing nodes when possible and produce a single sorted chain containing every node from the input lists exactly once.

Input Format

  • An array of kk linked-list heads.
  • Each linked list contains integer values in non-decreasing order.
  • Some lists may be empty.

Output Format

  • Return the head of one merged linked list in non-decreasing order.

Constraints

  • 0k0 \le k
  • The total number of nodes across all lists is finite.
  • Input lists are individually sorted in non-decreasing order.
  • If all lists are empty, return an empty list.
Examples
Sample cases returned by the problem API.

Example 1

Input

lists = [[1,4,5],[1,3,4],[2,6]]

Output

[1,1,2,3,4,4,5,6]

Explanation

Take the smallest available node from the heads of the lists until all nodes are merged into one sorted list.

Example 2

Input

lists = []

Output

[]

Explanation

There are no lists to merge, so the result is empty.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.