21,合并两个有序链表

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        rlt = ListNode(0)
        tmp = rlt
        while l1 or l2:
            if not l1:
                tmp.next = l2
                break
            if not l2:
                tmp.next = l1
                break
            if l1.val < l2.val:
                tmp.next = l1
                l1 = l1.next
            else:
                tmp.next = l2
                l2 = l2.next
            tmp = tmp.next
        return rlt.next

猜你喜欢

转载自blog.csdn.net/weixin_42758299/article/details/88588059