일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Generator
- 파이썬
- 715. Range Module
- Python Implementation
- kaggle
- Substring with Concatenation of All Words
- shiba
- 315. Count of Smaller Numbers After Self
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 컴퓨터의 구조
- 밴픽
- data science
- Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- attribute
- Regular Expression
- Protocol
- Python
- DWG
- 시바견
- concurrency
- LeetCode
- iterator
- t1
- Decorator
- Class
- Python Code
- 프로그래머스
Archives
- Today
- Total
Scribbling
LeetCode: 25. Reverse Nodes in k-Group 본문
Recursion solution.
Time Complexity: O(N), Memory Complexity: O(1)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
return self.helper(head, k)
def helper(self, node, k):
if not node:
return None
cur = node
for i in range(k-1):
cur = cur.next
if cur == None:
return node
next_node = cur.next
new_head = self.reverse(node, k)
node.next = self.helper(next_node, k)
return new_head
def reverse(self, node, k):
prv, cur = None, node
for i in range(k):
nxt = cur.next
cur.next = prv
prv = cur
cur = nxt
return prv
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 26. Remove Duplicates from Sorted Array (0) | 2021.08.27 |
---|---|
LeetCode: 24. Swap Nodes in Pairs (0) | 2021.08.26 |
LeetCode: 22. Generate Parentheses (0) | 2021.08.24 |
LeetCode: 19. Remove Nth Node From End of List (0) | 2021.08.22 |
LeetCode: 18. 4Sum (0) | 2021.08.22 |