题目描述
方法:
遍历加入node
# 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]:
pre = dump = ListNode(0)
while list1 or list2:
if not list1:
pre.next = list2
break
elif not list2:
pre.next = list1
break
if list1.val > list2.val:
pre.next = list2
list2 = list2.next
else:
pre.next = list1
list1 = list1.next
pre = pre.next
return dump.next