MergeTwoSortedLists

Published at 2021-11-16

# 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

    # l1에는 l1랑l2중 첫 값이 더 작은애로 계속 유지.
    # l1에 첫 값 놔두고 그 다음값들 부터 다시 정렬 ㄱㄱ