Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Merge K Sorted Linked Lists

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Merge K Sorted Linked Lists. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

At Meta, imagine multiple pre-sorted event streams need to be combined into one ordered feed. Given k sorted singly linked lists, merge them into one sorted linked list and return its head.

Formal Specification

Implement a function that takes lists, where lists[i] is the head of a sorted singly linked list or null. Each node contains an integer value and a next pointer. Return the head of a new merged sorted linked list formed by reusing the existing nodes.

Constraints

  • 0 <= k <= 10^4
  • 0 <= total number of nodes <= 10^5
  • -10^4 <= Node.val <= 10^4
  • Each linked list is sorted in non-decreasing order

Function Signature

def merge_k_lists(lists):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output