Leetcode: Merge two lists
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
root = ListNode()
current = root
while list1 is not None and list2 is not None:
if list1.val < list2.val:
current.next = ListNode(list1.val)
current = current.next
list1 = list1.next
else:
current.next = ListNode(list2.val)
current = current.next
list2 = list2.next
if list1 is not None:
current.next = list1
if list2 is not None:
current.next = list2
return root.next