File tree Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode:
3+ # def __init__(self, val=0, left=None, right=None):
4+ # self.val = val
5+ # self.left = left
6+ # self.right = right
7+ class Solution :
8+ def invertTree (self , root : Optional [TreeNode ]) -> Optional [TreeNode ]:
9+ if not root :
10+ return None
11+
12+ else :
13+ root .right , root .left = self .invertTree (root .left ), self .invertTree (root .right )
14+
15+ return root
16+
17+ ## TC: O(n), SC: O(n), avg O(logn) if the given tree is balanced
Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, x):
4+ # self.val = x
5+ # self.next = None
6+
7+ class Solution :
8+ def hasCycle (self , head : Optional [ListNode ]) -> bool :
9+ slow = head
10+ fast = head
11+
12+ while fast and fast .next :
13+ slow = slow .next
14+ fast = fast .next .next
15+ if slow == fast :
16+ return True
17+
18+ return False
19+
20+ # visited = head
21+
22+ # while visited:
23+ # if visited.val == None:
24+ # return True
25+
26+ # visited.val = None
27+ # visited = visited.next
28+
29+ # return False
30+
31+ # Both TC:O(n) and SC:O(1), but below one is kinda tricky solution
Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+ class Solution :
7+ def mergeTwoLists (self , list1 : Optional [ListNode ], list2 : Optional [ListNode ]) -> Optional [ListNode ]:
8+ if not list1 or not list2 :
9+ return list1 or list2
10+
11+ if list1 .val <= list2 .val :
12+ list1 .next = self .mergeTwoLists (list1 .next , list2 )
13+ return list1
14+
15+ else :
16+ list2 .next = self .mergeTwoLists (list1 , list2 .next )
17+ return list2
18+
19+ ## TC: O(n) or O(n+m), depends on the length of list1 and list2
20+ ## SC: O(max(m,n))
Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+ class Solution :
7+ def reverseList (self , head : Optional [ListNode ]) -> Optional [ListNode ]:
8+ prev , curr = None , head
9+
10+ while curr :
11+ tmp = curr .next ## save next node
12+ curr .next = prev ## reverse next pointer to the prev
13+ prev = curr ## update prev pointer with curr, since it's reversed now
14+ curr = tmp ## move on to the next saved node
15+
16+ return prev
17+
18+ ## TC: O(n) SC: O(1)
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def isValid (self , s : str ) -> bool :
3+
4+ d = {'(' : ')' , '[' : ']' , '{' : '}' }
5+ stack = []
6+
7+ for i in s :
8+ if i in d :
9+ stack .append (i )
10+ elif len (stack ) == 0 or d [stack .pop ()] != i :
11+ return False
12+
13+ return len (stack ) == 0
14+
15+ ## O(n) time complexity but not that much fast by leetcode system...
16+ ## Space complexity is also O(n)
You can’t perform that action at this time.
0 commit comments