一、leetcode地址
https://leetcode.com/problems/merge-two-sorted-lists/
二、问题描述
三、代码实现
语言:Python3
代码:
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
while l1 and l2:
if l1.val < l2.val:
head = l1
l1 = l1.next
res = self.mergeTwoLists(l1,l2)
head.next = res
else:
head = l2
l2 = l2.next
res = self.mergeTwoLists(l1,l2)
head.next = res
return head
if l1 == None:
return l2
if l2 == None:
return l1