Problems

Merge Two Sorted Lists

easy
easy
linked-list
two-pointers
recursion

Merge two ascending linked lists and return the head of one sorted node chain.

Constraints

  • 0 <= total nodes <= 100
  • -100 <= node value <= 100
  • Both lists are sorted

Follow-up

Compare iterative O(1)-space merging with recursive merging.

Execution note

Java/Go/Rust don't support this problem's ListNode/TreeNode structure yet — the starter is reference-only. Try Python or JavaScript to run and submit.

Examples

Example 1

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2

Input: list1 = [], list2 = []
Output: []

Example 3

Input: list1 = [], list2 = [0]
Output: [0]
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.