LeetCode 329

105. Construct Binary Tree from Preorder and Inorder Traversal

105. Construct Binary Tree from Preorder and Inorder Traversal https://youtu.be/ihj4IQGZ2zc?si=xp7MO4VGo3NIKjnW class Solution: def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: # 문제 접근법: # 1. Preorder 배열의 첫 번째 값은 항상 현재 서브트리의 루트 노드 값입니다. # 1. The first value in the preorder array is always the root node of the current subtree. # 2...

LeetCode/NeetCode 2024.12.11

86. Partition List

86. Partition List class Solution: def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]: """ x 기준값보다, 작은 더미 노드 리스트, 큰 더미 노드 리스트를 만들어서 이어주기 """ # 작은 값 리스트와 큰 값 리스트의 더미 노드 생성 small_dummy = ListNode(0) large_dummy = ListNode(0) # 포인터 초기화 small = small_dummy # 작은 값 리스트의 끝 large = large_dummy # ..

19. Remove Nth Node From End of List ☆★★★★

19. Remove Nth Node From End of List ✅ 올바른 풀이 (Two Pointer 방식 권장)아래는 리버스 없이 깔끔하게 해결할 수 있는 two pointer 방식입니다:class Solution: def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: # 더미 노드 설정 (head 제거될 경우 대비) dummy = ListNode(0, head) fast = slow = dummy # fast를 n+1만큼 먼저 이동시켜서 간격 만들기 # 왜? n 이동하고. dummy.next 부터 시작이니 1도 더함 ..

LeetCode/Grind169 2024.12.09

25. Reverse Nodes in k-Group

25. Reverse Nodes in k-Group https://youtu.be/1UOPsfP85V4?si=VL2go-L_JkxKBX53 # 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]: # 더미 노드 생성: head 이전에 추가해 리스트 시작점을 쉽게 다룰..