-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfd7843
commit 544837d
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
return prev | ||
|
||
## TC: O(n) SC: O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |