Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

81. Merge k Sorted Lists

HardDay 24 of 60Linked List

You are given an array of k linked lists, each already sorted in ascending order. Merge all of them into one sorted linked list and return its head. The array may be empty, and some of the lists may be empty.

Example 1
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Example 2
Input: lists = []
Output: []
Constraints
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • Total number of nodes N <= 10^4
Hints
Frequently asked at: Amazon, Meta, Google, Microsoft, Uber
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min