File tree Expand file tree Collapse file tree 5 files changed +113
-0
lines changed
Expand file tree Collapse file tree 5 files changed +113
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments