AlgoLounge

Merge Two Sorted Linked Lists

easy

Leetcode 21. Merge Two Sorted Lists

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted linked list and return the head of the new sorted linked list.

The new list should be made up of nodes from list1 and list2.

Examples

Example 1

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

Example 2

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

Constraints

  • 0 <= The length of the each list <= 100
  • -100 <= Node.val <= 100

How the tests work: lists are written as arrays — [1,2,3] is the chain 1 → 2 → 3. Your function receives real ListNode objects, and the list you return is read back into an array.

TypeScript
Loading editor…
List of test cases (10)
Case 1
list1 = [1,2,4]
list2 = [1,3,4]
Case 2
list1 = []
list2 = []
Case 3
list1 = []
list2 = [0]
Case 4
list1 = [1]
list2 = [2]
Case 5
list1 = [1,3,5]
list2 = [2,4,6]
Case 6
list1 = [1,1,1]
list2 = [2,2,2]
Case 7
list1 = [5]
list2 = [1,2,3,4]
Case 8
list1 = [-10,-5,0]
list2 = [-8,-2,4]
Case 9
list1 = [1,2,3]
list2 = []
Case 10
list1 = [1,100]
list2 = [50]