Back to Practice
#0273

Merge Two Sorted Linked Lists

EasyDSA18 min10 XP

Problem

Two linked lists are provided as sorted value arrays. Return the sorted values after merging them the same way you would merge two sorted linked lists node by node.

Why This Matters

Merging sorted lists is the core operation behind merge sort and many linked-list interview questions.

Function Signature

def merge_two_lists(a, b):

Examples

Example 1
Inputa = [1,2,4], b = [1,3,4]
Output[1, 1, 2, 3, 4, 4]

Always take the smaller current value, then append the remaining tail.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Two non-empty lists
Input[1,2,4], [1,3,4]
Expected[1, 1, 2, 3, 4, 4]

The values interleave in sorted order.

One empty list
Input[], [0]
Expected[0]

The remaining list becomes the answer.

Hidden Test Categories
duplicate valuesnegative valuesone list much longer