件名:
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
アイデア:
再帰的な方法で、結果を保存するための新しいリンクリストを作成します。
気分が良ければ、いいねするか、フォローするか、メッセージを残してください〜
ありがとうございます〜