Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Merge K Sorted Lists

Medium Acceptance 70.00% Points 30.00

You are given an array of k singly linked lists, where each linked list is sorted in non-decreasing order. Your task is to merge all the linked lists into one sorted linked list and return its head.

Examples
Example 1

Example 1

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

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

Explanation: All the sorted linked lists are merged into one sorted linked list.

Example 2

Example 2

Input: lists = []

Output: []

Explanation: Since there are no linked lists, the result is an empty linked list.

Example 3

Example 3

Input: lists = [[2],[1,3],[4,5]]

Output: [1,2,3,4,5]

Explanation: The nodes from all linked lists are merged while maintaining sorted order.

Hints
Hint 1
N/A
Constraints
  • 0 = k = 104
  • 0 = Total number of nodes = 105
  • -10? = Node.data = 10?
Companies
Visa
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions