Reverse Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Stack을 사용한 풀이 class Solution: def reverseList(self, head: ListNode) -> ListNode: if not head: return None stack = [] cur = head while cur: stack.append(cur) cur = cur.next new_head = stack.pop() cur = new_head..
Palindrome Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 단순한 풀이 class Solution: def isPalindrome(self, head: ListNode) -> bool: cur = head trace = [] while cur: trace.append(cur.val) cur = cur.next return trace == trace[::-1] 경로를 저장하고 경로와 뒤집은 경로가 같은지를 반환한다. 2. Po..
Invert Binary Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Queue를 이용한 풀이 from collections import deque class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return None if not root.left and not root.right: return root q = deque([]) q.append(roo..
Linked List Cycle - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. Object id값과 집합을 이용한 풀이 class Solution: def hasCycle(self, head: ListNode) -> bool: id_set = set() cur = head while cur: if id(cur) not in id_set: id_set.add(id(cur)) else: return True cur = cur.next return False..
Maximum Subarray - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. prefix sum을 이용한 풀이 class Solution: def maxSubArray(self, nums: List[int]) -> int: if len(nums) == 1: return sum(nums) prefix = [nums[0]] for index, num in enumerate(nums): if index == 0: continue prefix.append(nu..
Symmetric Tree - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. in-order 접근(실패) class Solution: def isSymmetric(self, root: TreeNode) -> bool: trace = [] def dfs(cur): if not cur: trace.append(None) else: if not (not cur.left and not cur.right): dfs(cur.left) trace.append(cur.v..