본문 바로가기

프로그래머/Python

[Python] Leet Code 21. Merge Two Sorted Lists

본 내용은 <파이썬 알고리즘 인터뷰>를 참고했습니다.

리스트 변환

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:

        if (not l1) or (l2 and l2.val < l1.val):
            l1, l2 = l2, l1

        if l1:
            l1.next = self.mergeTwoLists(l1.next, l2)
        return l1

이 문제에서 배워야할 포인트

  • 재귀활용!!
  • python의 swap
    • 작은 값을 무조건 l1에