Problem can be found in here!
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def mergeTwoLists(list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy_head = current_node = ListNode()
while list1 and list2:
if list1.val < list2.val:
current_node.next = list1
list1 = list1.next
else:
current_node.next = list2
list2 = list2.next
current_node = current_node.next
current_node.next = list1 if list1 else list2
return dummy_head.next
Time Complexity: , Space Complexity: , where n and m is the length of the linked list list1 and list2, respectively.