일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- t1
- data science
- 컴퓨터의 구조
- Generator
- concurrency
- 프로그래머스
- Substring with Concatenation of All Words
- 715. Range Module
- Convert Sorted List to Binary Search Tree
- Decorator
- Python Code
- attribute
- DWG
- 109. Convert Sorted List to Binary Search Tree
- 파이썬
- Python Implementation
- 43. Multiply Strings
- Regular Expression
- 315. Count of Smaller Numbers After Self
- iterator
- 30. Substring with Concatenation of All Words
- Protocol
- Python
- LeetCode
- 운영체제
- 시바견
- Class
- kaggle
- shiba
- 밴픽
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 |