首页 > 其他分享 >剑指25. 合并两个有序链表

剑指25. 合并两个有序链表

时间:2023-01-04 11:55:16浏览次数:61  
标签:25 None return self next 链表 l2 有序 l1

问题描述

https://leetcode.cn/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/description/

解题思路

参考这个

代码

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1, l2):
        if l1 is None and l2 is None:
            return None
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1, l2.next)
            return l2

 

标签:25,None,return,self,next,链表,l2,有序,l1
From: https://www.cnblogs.com/bjfu-vth/p/17024428.html

相关文章