Subject:
python code:
# 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
Idea:
Create a new linked list to store the results, through a recursive method.
If you feel good, please like or follow or leave a message~
Thank you~