class Solution:
def mergeTwoLists(self, list1, list2):
result = None
if not list1:
return list2
elif not list2:
return list1
elif not list1 and not list2:
return result
if list1 and list2:
if list1.val > list2.val:
result = list2
list2 = list2.next
else:
result = list1
list1 = list1.next
head = result
while list1 and list2:
if list1.val > list2.val:
result.next = list2
result = result.next
list2 = list2.next
else:
result.next = list1
result = result.next
list1 = list1.next
if list1:
result.next = list1
else:
result.next = list2
return head
문제