Skip to main content
CodeOath
← All problems

Problem

Merge Two Sorted Lists

Easy
  • linked-list

Combine the sorted linked lists l1 and l2 into one sorted list and return its head. Do it by relinking the existing nodes, not by creating new ones. Equal values are all kept.

Lists in this problem are given and returned as a plain array of values, in order.

Example 1
Input
l1 = [2, 5, 9], l2 = [1, 5, 6, 10]
Output
[1, 2, 5, 5, 6, 9, 10]
Explanation

the values are taken smallest first, and both 5s stay in the result.

Example 2
Input
l1 = [], l2 = [7, 8]
Output
[7, 8]
Explanation

when one list is empty, the result is the other list.

Constraints:

  • both lists are sorted in non-decreasing order
  • either list, or both, may be empty

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.