Skip to content

Commit a3fc68a

Browse files
authored
Merge pull request DaleStudy#49 from SamTheKorean/solution2
[Sam] Week 2 solutions
2 parents 71d9c80 + 955d3d2 commit a3fc68a

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

invert-binary-tree/SamTheKorean.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time complexity : O(n)
2+
# Space complexity : O(n)
3+
class Solution:
4+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
5+
stack = [root]
6+
7+
while stack:
8+
node = stack.pop()
9+
10+
# Not accessing child if node is Null
11+
if not node:
12+
continue
13+
14+
node.left, node.right = node.right, node.left
15+
stack += [node.left, node.right]
16+
17+
return root

linked-list-cycle/SamTheKorean.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Time complexity : O(n)
2+
# Space complexity : O(1)
3+
class Solution:
4+
def hasCycle(self, head: Optional[ListNode]) -> bool:
5+
slow = head
6+
fast = head
7+
8+
# check fast and fast.next are not the last node to ensure we can access fast.next.next
9+
while fast and fast.next:
10+
slow = slow.next
11+
fast = fast.next.next
12+
if slow == fast:
13+
return True
14+
15+
return False
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time complexity : O(m*n)
2+
# Space complexity : O(1)
3+
class Solution:
4+
def mergeTwoLists(
5+
self, list1: Optional[ListNode], list2: Optional[ListNode]
6+
) -> Optional[ListNode]:
7+
# Create a dummy node to simplify the logic
8+
dummy = ListNode(None)
9+
# Pointer to the current node in the merged list
10+
node = dummy
11+
12+
# Loop until both lists have nodes
13+
while list1 and list2:
14+
# Choose the smaller value between list1 and list2
15+
if list1.val < list2.val:
16+
# Append list1 node to the merged list
17+
node.next = list1
18+
# Move to the next node in list1
19+
list1 = list1.next
20+
else:
21+
# Append list2 node to the merged list
22+
node.next = list2
23+
# Move to the next node in list2
24+
list2 = list2.next
25+
# Move to the next node in the merged list
26+
node = node.next
27+
28+
# Append the remaining nodes from list1 or list2
29+
node.next = list1 or list2
30+
31+
# Return the merged list (skip the dummy node)
32+
return dummy.next

reverse-linked-list/SamTheKorean.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time complexity : O(n)
2+
# Space complexity : O(n)
3+
class Solution:
4+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
5+
# Return None when head is None
6+
if not head:
7+
return None
8+
9+
stack = []
10+
node = head
11+
12+
while node:
13+
stack.append(node)
14+
node = node.next
15+
16+
head = stack.pop()
17+
node = head
18+
19+
# End the loop when stack is empty
20+
while stack:
21+
node.next = stack.pop()
22+
node = node.next
23+
24+
# Set the last node's next to None to indicate the end of the reversed list
25+
node.next = None
26+
27+
return head

valid-parentheses/SamTheKorean.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time complexity : O(n)
2+
# Space complexity : O(n)
3+
class Solution:
4+
def isValid(self, s: str) -> bool:
5+
parentheses = {"[": "]", "{": "}", "(": ")"}
6+
stack = []
7+
8+
# if ch is opening bracket
9+
for ch in s:
10+
if ch in parentheses:
11+
stack.append(ch)
12+
# if ch is closing bracket
13+
else:
14+
# if closing bracket comes without opening bracket previously
15+
if not stack:
16+
return False
17+
# if closing bracket doesnt match with the latest type of opening bracket
18+
if ch != parentheses[stack.pop()]:
19+
return False
20+
21+
# True if stack is empty after going through the process above
22+
return not stack

0 commit comments

Comments
 (0)