일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 매일
- 10분
- 30분
- 리얼 클래스
- 프로젝트
- 미드시청
- 개발자
- 사이드
- 영어원서읽기
- 운동
- Writing
- 만화도
- 뭐든
- 영어공부
- Daily Challenge
- 화상영어
- 괜찮음
- English
- FIT XR
- leetcode
- 월간
- 스탭퍼
- 잡생각
- Problem Solving
- 파비최
- 3줄정리
- 쓰릴오브파이트
- 링피트
- realclass
- 읽기
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.09.06 Today's Challenge 본문
https://leetcode.com/problems/split-linked-list-in-parts/
Split Linked List in Parts - LeetCode
Can you solve this real interview question? Split Linked List in Parts - Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts. The length of each part should be as equal as possible: no two par
leetcode.com
리스트의 연속
class Solution:
def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
cur = head
for n in range(1001):
if not cur:
break
cur = cur.next
width, remainder = divmod(n, k)
ans = []
cur = head
for i in range(k):
head = write = ListNode(None)
for j in range(width + (i < remainder)):
write.next = write = ListNode(cur.val)
if cur:
cur = cur.next
ans.append(head.next)
return ans
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.09.08 Today's Challenge (0) | 2023.09.08 |
---|---|
2023.09.07 Today's Challenge (0) | 2023.09.07 |
2023.09.05 Today's Challenge (0) | 2023.09.05 |
2023.09.04 Today's Challenge (0) | 2023.09.04 |
2023.09.03 Today's Challenge (0) | 2023.09.03 |
Comments