diff --git a/invert-binary-tree/SamTheKorean.py b/invert-binary-tree/SamTheKorean.py new file mode 100644 index 000000000..8ed773b8c --- /dev/null +++ b/invert-binary-tree/SamTheKorean.py @@ -0,0 +1,17 @@ +# Time complexity : O(n) +# Space complexity : O(n) +class Solution: + def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + stack = [root] + + while stack: + node = stack.pop() + + # Not accessing child if node is Null + if not node: + continue + + node.left, node.right = node.right, node.left + stack += [node.left, node.right] + + return root diff --git a/linked-list-cycle/SamTheKorean.py b/linked-list-cycle/SamTheKorean.py new file mode 100644 index 000000000..086bc3983 --- /dev/null +++ b/linked-list-cycle/SamTheKorean.py @@ -0,0 +1,15 @@ +# Time complexity : O(n) +# Space complexity : O(1) +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + slow = head + fast = head + + # check fast and fast.next are not the last node to ensure we can access fast.next.next + while fast and fast.next: + slow = slow.next + fast = fast.next.next + if slow == fast: + return True + + return False diff --git a/merge-two-sorted-lists/SamTheKorean.py b/merge-two-sorted-lists/SamTheKorean.py new file mode 100644 index 000000000..4015c9bc5 --- /dev/null +++ b/merge-two-sorted-lists/SamTheKorean.py @@ -0,0 +1,32 @@ +# Time complexity : O(m*n) +# Space complexity : O(1) +class Solution: + def mergeTwoLists( + self, list1: Optional[ListNode], list2: Optional[ListNode] + ) -> Optional[ListNode]: + # Create a dummy node to simplify the logic + dummy = ListNode(None) + # Pointer to the current node in the merged list + node = dummy + + # Loop until both lists have nodes + while list1 and list2: + # Choose the smaller value between list1 and list2 + if list1.val < list2.val: + # Append list1 node to the merged list + node.next = list1 + # Move to the next node in list1 + list1 = list1.next + else: + # Append list2 node to the merged list + node.next = list2 + # Move to the next node in list2 + list2 = list2.next + # Move to the next node in the merged list + node = node.next + + # Append the remaining nodes from list1 or list2 + node.next = list1 or list2 + + # Return the merged list (skip the dummy node) + return dummy.next diff --git a/reverse-linked-list/SamTheKorean.py b/reverse-linked-list/SamTheKorean.py new file mode 100644 index 000000000..822ed3c08 --- /dev/null +++ b/reverse-linked-list/SamTheKorean.py @@ -0,0 +1,27 @@ +# Time complexity : O(n) +# Space complexity : O(n) +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + # Return None when head is None + if not head: + return None + + stack = [] + node = head + + while node: + stack.append(node) + node = node.next + + head = stack.pop() + node = head + + # End the loop when stack is empty + while stack: + node.next = stack.pop() + node = node.next + + # Set the last node's next to None to indicate the end of the reversed list + node.next = None + + return head diff --git a/valid-parentheses/SamTheKorean.py b/valid-parentheses/SamTheKorean.py new file mode 100644 index 000000000..2be226432 --- /dev/null +++ b/valid-parentheses/SamTheKorean.py @@ -0,0 +1,22 @@ +# Time complexity : O(n) +# Space complexity : O(n) +class Solution: + def isValid(self, s: str) -> bool: + parentheses = {"[": "]", "{": "}", "(": ")"} + stack = [] + + # if ch is opening bracket + for ch in s: + if ch in parentheses: + stack.append(ch) + # if ch is closing bracket + else: + # if closing bracket comes without opening bracket previously + if not stack: + return False + # if closing bracket doesnt match with the latest type of opening bracket + if ch != parentheses[stack.pop()]: + return False + + # True if stack is empty after going through the process above + return not stack