def mergeTwoLists(self, l1, l2):
'''
新建一个链表,头指针保存,用来存储新的有序链表
'''
if not l1 and not l2:
return None
head = ListNode(0) ###定义链表的头指针
first = head ###头指针
while l1 and l2:
if l1.val > l2.val:
head.next = l2
l2 = l2.next
else:
head.next = l1
l1 = l1.next
####head指针代表下一次要插入节点位置的前一个
head = head.next
if l1:
head.next = l1
if l2:
head.next = l2
return first.next
和leetcode88类似