📖이 포스팅은 '파이썬 알고리즘 인터뷰 - 박상길님'을 보고 작성되었습니다.
😎문제 : https://leetcode.com/problems/merge-two-sorted-lists/
정렬되어 있는 두 연결 리스트를 합쳐라
[재귀 구조로 연결]
# 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
'자료구조와 알고리즘 > 🥑알고리즘' 카테고리의 다른 글
[알고리즘][연결 리스트] - 4. 두 수의 덧셈 (0) | 2022.02.26 |
---|---|
[알고리즘][연결 리스트] - 3. 역순 연결 리스트 (0) | 2022.02.26 |
[알고리즘][연결 리스트] - 1. 팰린드롬 연결리스트 (0) | 2022.02.24 |
[알고리즘][배열] - 7. 주식을 사고 팔기 가장 좋은 시점 (0) | 2022.02.21 |
[알고리즘][배열] - 6. 자신을 제외한 배열의 곱 (0) | 2022.02.21 |