您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Leetcode-21题:Merge Two Sorted Li

Leetcode-21题:Merge Two Sorted Li

来源:伴沃教育

题目:

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

代码:

def mergeTwoLists(self, l1, l2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    head = ListNode(None)
    p = head

    while l1!=None and l2!=None:
        p.next = l1
        if l1.val < l2.val:
            p = p.next
            l1 = l1.next
        else:
            p.next = l2
            p = p.next
            l2 = l2.next

    if l1 != None:
        p.next = l1
    if l2 != None:
        p.next = l2

    return head.next

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务