ntree.ai
ntree.aiMerge Two Sorted Lists

Merge Two Sorted Lists

easysolvedattempted

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

Join them into one sorted list by relinking the nodes of the two lists, and return the head of it.

Example 1:

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

Example 2:

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

Constraints:

  • 0 <= number of nodes in each list <= 50
  • -100 <= Node.val <= 100
# Definitions:
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
 
def mergeTwoLists(list1: ListNode, list2: ListNode) -> ListNode:
pass
 
1→2→4
1→3→4

Press Visualise to step through your code.