Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Coding: Linked List Merge

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

Your question is Coding: Linked List Merge. 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

A GE HealthCare Edison imaging workflow may maintain sorted linked lists of work items from two processing streams. Given the heads of two singly linked lists sorted in nondecreasing order, merge them into one sorted linked list.

Formal Specification

Implement merge_sorted_lists(l1, l2), where l1 and l2 are either ListNode objects or None. Each ListNode has an integer val field and a next field. Return the head of the merged list. Reuse the existing nodes rather than creating nodes for every output value. If values are equal, either source list may be selected first.

For the JSON test cases, each input list is represented by an array of node values, and the expected result is the merged array of values.

Constraints

  • 0 <= len(l1), len(l2) <= 10^5
  • -10^9 <= node.val <= 10^9
  • Each input list is sorted in nondecreasing order
  • Input nodes form finite, acyclic singly linked lists

Function Signature

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