문제
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
풀이
Divide & Conquer 전략에 따라 작은 문제를 해결하고, 이를 조합하여 큰 문제를 해결한다.
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if l1 is None :
return l2
if l2 is None :
return l1
if (l1.val > l2.val) :
head = ListNode(l2.val)
head.next = self.mergeTwoLists(l1, l2.next)
else :
head = ListNode(l1.val)
head.next = self.mergeTwoLists(l1.next, l2)
return head
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
100. Same Tree (0) | 2020.05.11 |
---|---|
88. Merge Sorted Array (0) | 2020.05.11 |
83. Remove Duplicates from Sorted List (0) | 2020.05.09 |
70. Climbing Stairs (0) | 2020.05.08 |
69. Sqrt(x) (0) | 2020.05.08 |