# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defmergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# step1: create a dummy head to store the first result node # step2: compare the current node of the two linked lists, and add the samller one to the end of dummy list # stpe3: return dummy list's next pointer # time complexity: O(m + n) # space complexity: O(1)