From b267714124910b8976d7a4bde644c4aab6561403 Mon Sep 17 00:00:00 2001 From: Shaikh Israil Date: Sat, 31 Jan 2026 21:12:34 -0800 Subject: [PATCH 1/4] exercise 1 --- Exercise_1.py | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..28dfe2556 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,34 @@ +# Time Complexity : Push: O(1), POP: O(1), Peek: O(1), size: O(1) + +# Space Complexity : O(N) + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file - def __init__(self): - - def isEmpty(self): - - def push(self, item): - - def pop(self): - - - def peek(self): + def __init__(self): + self.arr = [] + + def isEmpty(self): + if len(self.arr) == 0: + return True + return False + + def push(self, item): + self.arr.append(item) + print(self.arr) + + def pop(self): + return self.arr.pop() + + def peek(self): + if self.arr: + return self.arr[-1] + + def size(self): + return len(self.arr) - def size(self): - - def show(self): + def show(self): + return self.arr s = myStack() From 92d4b07ad9e36bc379aab1dcd566c976f6458de3 Mon Sep 17 00:00:00 2001 From: Shaikh Israil Date: Sat, 31 Jan 2026 21:20:27 -0800 Subject: [PATCH 2/4] Excercise 2 --- Exercise_2.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..81438bba7 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,6 @@ +# Time Complexity : push: O(1) +# Space Complexity : pop: O(1) class Node: def __init__(self, data): self.data = data @@ -6,10 +8,13 @@ def __init__(self, data): class Stack: def __init__(self): + self.arr = [] def push(self, data): + self.arr.append(data) def pop(self): + return self.arr.pop() a_stack = Stack() while True: From 4f64df35982e85936b8744fd1a2dc7046c17a642 Mon Sep 17 00:00:00 2001 From: Shaikh Israil Date: Sun, 8 Feb 2026 15:36:15 -0800 Subject: [PATCH 3/4] Exercise 3- Linked list impl --- Exercise_3.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..62d66b0fb 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,7 @@ class ListNode: A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.val = data class SinglyLinkedList: def __init__(self): @@ -10,13 +11,18 @@ def __init__(self): Create a new singly-linked list. Takes O(1) time. """ - self.head = None + self.head = ListNode(-1) def append(self, data): """ Insert a new element at the end of the list. Takes O(n) time. """ + curr = self.head + node = ListNode(data) + while curr.next != None: + curr = curr.next + curr.next = node def find(self, key): """ @@ -24,9 +30,22 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + curr = self.head.next + while curr: + if curr.data == key: + return curr + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + curr = self.head.next + prev = None + while curr and curr.data != key: + prev = curr + curr = curr.next + if prev: + prev.next = curr.next + return From 2488b9530a2f342e9fea2d6ddc26fcb37e9f4c5e Mon Sep 17 00:00:00 2001 From: Shaikh Israil Date: Sun, 8 Feb 2026 16:00:10 -0800 Subject: [PATCH 4/4] implement using linkedlist - Ex 2 --- Exercise_2.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Exercise_2.py b/Exercise_2.py index 81438bba7..71ae550b7 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -8,13 +8,18 @@ def __init__(self, data): class Stack: def __init__(self): - self.arr = [] + self.head = Node(-1) def push(self, data): - self.arr.append(data) + node = Node(data) + node.next = self.head.next + self.head.next = node def pop(self): - return self.arr.pop() + curr = self.head.next + if curr: + self.head.next = curr.next + return curr.data if curr else None a_stack = Stack() while True: