class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
dummy = ListNode()
tail = dummy
while(list1 is not None and list2 is not None):
if list1.val <= list2.val:
tail.next = list1
list1 = list1.next
else:
tail.next = list2
list2 = list2.next
tail = tail.next
if list1 is not None:
tail.next = list1
elif list2 is not None:
tail.next = list2
return dummy.next
Anyone know why tail.next = list appends 1 node tail while the last if statement can append the remaining nodes? I'm so confused on this part