Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Merge Two Sorted Linked Lists

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

Your question is Merge Two 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

Given the heads of two singly linked lists list1 and list2, where each list is sorted in non-decreasing order, merge them into a single sorted linked list and return its head. The merged list should be built by reusing the existing nodes, not by creating a separate list of values.

Constraints

  • 0 <= number of nodes in each list <= 50
  • -100 <= Node.val <= 100
  • Both input lists are sorted in non-decreasing order

Function Signature

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