728x90
LeetCode-21
Merge Two Sorted Lists : Merge Two lists
note : recursive
:::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, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if (not list1) or (list2 and list1.val > list2.val):
list1, list2 = list2, list1
if list1:
list1.next = self.mergeTwoLists(list1.next, list2)
return list1
Result : 42ms Memory: 13.8mb
๋ฐ์ํ
'๐ข One step' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[leetcode-2] Add Two Numbers (0) | 2023.03.17 |
---|---|
[leetcode-206] Reverse Linked List (0) | 2023.03.15 |
[leetcode 234] Palindrom Linked List (0) | 2023.03.11 |
[leetcode-121] Best Time to Buy and Sell Stock (0) | 2023.03.09 |
[leetcode-238] Product of Array Except Self (0) | 2023.03.08 |