链表,这个我也是第一次碰到,知道他和列表的区别。就借助这个练习来了解。
https://leetcode.com/problems/merge-two-sorted-lists/
代码不复杂
# 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, l1: ListNode, l2: ListNode) -> ListNode: 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
慢慢再理解。