Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions invert-binary-tree/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None

else:
root.right, root.left = self.invertTree(root.left), self.invertTree(root.right)

return root

## TC: O(n), SC: O(n), avg O(logn) if the given tree is balanced
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

훌륭한 복잡도 분석이네요! 💯

31 changes: 31 additions & 0 deletions linked-list-cycle/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = head
fast = head

while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True

return False

# visited = head

# while visited:
# if visited.val == None:
# return True

# visited.val = None
# visited = visited.next

# return False

# Both TC:O(n) and SC:O(1), but below one is kinda tricky solution
20 changes: 20 additions & 0 deletions merge-two-sorted-lists/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list1 or not list2:
return list1 or list2

if list1.val <= list2.val:
list1.next = self.mergeTwoLists(list1.next, list2)
return list1

else:
list2.next = self.mergeTwoLists(list1, list2.next)
return list2

## TC: O(n) or O(n+m), depends on the length of list1 and list2
## SC: O(max(m,n))
18 changes: 18 additions & 0 deletions reverse-linked-list/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev, curr = None, head

while curr:
tmp = curr.next ## save next node
curr.next = prev ## reverse next pointer to the prev
prev = curr ## update prev pointer with curr, since it's reversed now
curr = tmp ## move on to the next saved node
Comment on lines +10 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while curr:
tmp = curr.next ## save next node
curr.next = prev ## reverse next pointer to the prev
prev = curr ## update prev pointer with curr, since it's reversed now
curr = tmp ## move on to the next saved node
while curr:
curr.next, prev, curr = prev, curr, curr.next

tmp 사용 없이 이렇게 하는 방법도 있을 것 같아요. ☺️

Copy link
Contributor Author

@leokim0922 leokim0922 May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!! 포인터 문제들은 늘 할때마다 헷갈려서 tmp 해놓은거도 있고 안해놓은거도 있고 뒤죽박죽이네요 ㅎㅎ 좀 일관성있게 작성하도록 노력해보겠습니다 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 파이썬은 temp 없이 스왑이 되는군요...!


return prev

## TC: O(n) SC: O(1)
16 changes: 16 additions & 0 deletions valid-parentheses/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def isValid(self, s: str) -> bool:

d = {'(': ')', '[': ']', '{': '}'}
stack = []

for i in s:
if i in d:
stack.append(i)
elif len(stack) == 0 or d[stack.pop()] != i:
return False

return len(stack) == 0

## O(n) time complexity but not that much fast by leetcode system...
## Space complexity is also O(n)