Skip to main content

Merge Linked Lists

HardPremium

Merge K Sorted Linked Lists. You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Your task is to merge all the linked-lists into one sorted linked-list and return it.

Examples:

  1. If lists = [[1,4,5],[1,3,4],[2,6]], where each array represents a linked-list, the merged and sorted linked-list would be [1,1,2,3,4,4,5,6]. The output for the merged list should be in array form for simplicity.
  2. If lists = [], indicating an array of no linked-lists, the output should be [].
  3. If lists = [[]], representing an array with a single empty linked-list, the output should be [].
  4. If lists = [[],[1]], where the first linked-list is empty and the second contains a single element, the output should be [1].
  5. If lists = [[-1,5,11],[],[6,10]], the merged and sorted linked-list should be [-1,5,6,10,11].

Constraints:

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • 10^4 <= lists[i][j] <= 10^4
  • lists[i] is sorted in an increasing order.