
1.链表判空
2.挨个比大小插入新的链表
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l3 = ListNode(0)
head = l3
while l1 != None or l2 != None:
if l1.val > l2.val:
head.next = l2.next
l2 = l2.next
else:
head.next = l1
l1 = l1.next
head = head.next
if l1 == None:
head.next = l2
elif l2 == None:
head.next = l1
return head.next
1.注意判断时候:if l1.val > l2.val: 如果改为l1>l2 则会变为判断两个链表长短
526

被折叠的 条评论
为什么被折叠?



