알고리즘
[파이썬 알고리즘 인터뷰] 두 정렬 리스트의 병합
고구마뿌리
2022. 2. 22. 19:37
728x90
Merge Two Sorted Lists - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
재귀 구조로 연결
def mergeTwoLists(self, l1,l2) -> 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
재귀를 이용해서 푸는 건데... 솔직히 이해가 잘 되지 않는다... 재귀를 이용한 코드를 짜는 것조차 솔직히 지금은 많이 버거운것 같다... 이해하면 다시 돌아와서 정리해야겠다.
반응형