## 題解 ```python= # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: output = ListNode() cur = output while list1 and list2: if list1.val < list2.val: temp = list1.next list1.next = None cur.next = list1 list1 = temp else: temp = list2.next list2.next = None cur.next = list2 list2 = temp cur = cur.next while list1: temp = list1.next list1.next = None cur.next = list1 list1 = temp cur = cur.next while list2: temp = list2.next list2.next = None cur.next = list2 list2 = temp cur = cur.next return output.next ``` 2023.08.30 AC ```python= # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: output = ListNode() cur = output c1 = list1 c2 = list2 while c1 and c2: if c1.val < c2.val: cur.next = c1 c1 = c1.next else: cur.next = c2 c2 = c2.next cur = cur.next if c1: cur.next = c1 else: cur.next = c2 return output.next ```