Asked at

Merge Two Sorted Lists

Easy
Verified
Linked List~15 min

Given the values of two sorted singly linked lists l1 and l2 (each head first), merge them into one sorted list and return its values.

Walk both lists with two pointers, always taking the smaller head. Append the remaining tail once either list runs out.

The input arrives as a single object { l1, l2 }.

Examples

in{ l1: [1, 2, 4], l2: [1, 3, 4] }
out[1, 1, 2, 3, 4, 4]

Splice the two sorted lists into one sorted order.

in{ l1: [], l2: [] }
out[]

Two empty lists merge to an empty list.

Constraints

  • The number of nodes in each list is in the range [0, 50].
  • -100 ≤ Node.val ≤ 100
  • Both lists are sorted in non-decreasing order.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.