https://leetcode.com/problems/merge-two-sorted-lists/
Merge Two Sorted Lists - LeetCode
Merge Two Sorted Lists - You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
leetcode.com
1
2
3
4
5
6
7
8
9
10
11
12
|
# 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: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
if (not l1) or (l2 and l1.val > l2.val):
l1, l2 = l2, l1
if l1:
l1.next = self.mergeTwoLists(l1.next, l2)
return l1
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
백준 9772 Quadrants (0) | 2023.02.17 |
---|---|
leetcode 989. Add to Array-Form of Integer (0) | 2023.02.15 |
leetcode 1523. Count Odd Numbers in an Interval Range (0) | 2023.02.13 |
leetcode 234. Palindrome Linked List (0) | 2023.02.11 |
leetcode 121. Best Time to Buy and Sell Stock (0) | 2023.02.11 |
댓글