일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- leetcode
- 리얼 클래스
- 괜찮음
- 운동
- 뭐든
- 화상영어
- English
- realclass
- 링피트
- Daily Challenge
- 파비최
- Writing
- 30분
- 미드시청
- 매일
- 사이드
- 3줄정리
- 개발자
- 읽기
- 영어원서읽기
- 월간
- FIT XR
- 쓰릴오브파이트
- 만화도
- 영어공부
- 프로젝트
- Problem Solving
- 10분
- 스탭퍼
- 잡생각
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.03.12 Today's Challenge 본문
https://leetcode.com/problems/merge-k-sorted-lists/
Merge k Sorted Lists - LeetCode
Can you solve this real interview question? Merge k Sorted Lists - You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1: Input: lis
leetcode.com
머지머지 머지하는 소트소트소트
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
if not lists:
return None
if len(lists) == 1:
return lists[0]
mid = len(lists) // 2
left = self.mergeKLists(lists[:mid])
right = self.mergeKLists(lists[mid:])
return self.merge(left, right)
def merge(self, l1, l2):
dummy = ListNode(0)
curr = dummy
while l1 and l2:
if l1.val < l2.val:
curr.next = l1
l1 = l1.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
curr.next = l1 or l2
return dummy.next
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.03.14 Today's Challenge (0) | 2023.03.14 |
---|---|
2023.03.13 Today's Challenge (0) | 2023.03.13 |
2023.03.11 Today's Challenge (0) | 2023.03.11 |
2023.03.10 Today's Challenge (0) | 2023.03.10 |
2023.03.09 Today's Challenge (0) | 2023.03.09 |
Comments