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