题目:
python 代码:
# 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:
listNew = ListNode(None)
if l1 == None:
return l2
if l2 == None:
return l1
if l1.val <= l2.val:
listNew = l1
listNew.next = self.mergeTwoLists(l1.next, l2)
else:
listNew = l2
listNew.next = self.mergeTwoLists(l1, l2.next)
return listNew
思路:
创建一个新的链表,来存储结果, 通过递归的方法。
如果觉得不错,就点赞或者关注或者留言~
谢谢~