Skip to content

Latest commit

 

History

History
28 lines (26 loc) · 882 Bytes

10. 合并两个有序链表.md

File metadata and controls

28 lines (26 loc) · 882 Bytes

将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
# 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]:
        # 终止条件,直到两个链表都空
        if not list1:
            return list2
        if not list2:
            return list1
        # 递归调用
        if list1.val < list2.val:
            list1.next = self.mergeTwoLists(list1.next, list2)
            return list1
        else:
            list2.next = self.mergeTwoLists(list1, list2.next)
            return list2