From 635a601f217e43b69449b21ea5e53979279f9a5b Mon Sep 17 00:00:00 2001 From: spencerkrebs Date: Sun, 5 Apr 2026 20:10:32 -0400 Subject: [PATCH 1/2] Exercise 1 stack --- Exercise_1.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..2a057748d 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,38 @@ +# // Time Complexity : +# // Space Complexity : +# // Did this code successfully run on Leetcode : +# // Any problem you faced while coding this : + + +# // Your code here along with comments explaining your approach + class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): + self.stack = [] def isEmpty(self): + return len(self.stack) == 0 def push(self, item): + self.stack.append(item) def pop(self): - - + return self.stack.pop(-1) + def peek(self): + return self.stack[-1] def size(self): + return len(self.stack) def show(self): + return self.stack s = myStack() s.push('1') s.push('2') print(s.pop()) -print(s.show()) +print(s.show()) \ No newline at end of file From ad0ed116b0d7ef9381f4d9d45814663d64e98892 Mon Sep 17 00:00:00 2001 From: spencerkrebs Date: Sun, 5 Apr 2026 20:57:56 -0400 Subject: [PATCH 2/2] exercise 1,2,3 --- Exercise_1.py | 6 +++--- Exercise_2.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++- Exercise_3.py | 51 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 4 deletions(-) diff --git a/Exercise_1.py b/Exercise_1.py index 2a057748d..bb2948e85 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,8 +1,8 @@ -# // Time Complexity : -# // Space Complexity : +# // Time Complexity : O(n) +# // Space Complexity : O(n) # // Did this code successfully run on Leetcode : # // Any problem you faced while coding this : - +# used stack.push rather than stack.append. append is used to push to array in python # // Your code here along with comments explaining your approach diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..383612f1c 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,13 @@ +# // Time Complexity : O(1) +# // Space Complexity : O(n) +# // Did this code successfully run on Leetcode : +# // Any problem you faced while coding this : +# did it in O(n) first but can be done in O(1) time. add at begining of linked list, always add to front of list - thats top of stack + +# // Your code here along with comments explaining your approach + + class Node: def __init__(self, data): self.data = data @@ -6,11 +15,60 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = None def push(self, data): - + newNode = Node(data) + if not self.head: + self.head = newNode + else: + newNode.next = self.head + self.head = newNode + def pop(self): + if self.head is None: + return None + poppedNode = self.head + self.head = self.head.next + return poppedNode.data + + + +# this is O(n) but can be done in O(1) above +# class Node: +# def __init__(self, data): +# self.data = data +# self.next = None + +# class Stack: +# def __init__(self): +# self.head = None +# def push(self, data): +# newNode = Node(data) +# if self.head == None: +# self.head = newNode +# else: +# cur = self.head +# while cur.next: +# cur = cur.next + +# cur.next = newNode + +# def pop(self): +# cur = self.head + +# while cur.next.next: +# cur = cur.next + +# poppedNode = cur.next +# cur.next = None +# return poppedNode.data + + +# 1 -> 2 -> 3 +# c c + a_stack = Stack() while True: #Give input as string if getting an EOF error. Give input like "push 10" or "pop" diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..885d64311 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,22 @@ + +# // Time Complexity : O(n) +# // Space Complexity : O(n) +# // Did this code successfully run on Leetcode : +# // Any problem you faced while coding this : +# - forgot the else condition in append method +# - for remove, you need to make sure you can handle the head node or last node being the node to delete, and also account for node not being found + + +# // Your code here along with comments explaining your approach + + class ListNode: """ A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.data = data + self.next = next class SinglyLinkedList: def __init__(self): @@ -17,6 +31,15 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + if self.head is None: + self.head = ListNode(data) + else: + cur = self.head + while cur.next: + cur = cur.next + + cur.next = ListNode(data) + def find(self, key): """ @@ -24,9 +47,37 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + cur = self.head + while cur: + if cur.data == key: + return cur + cur = cur.next + return None + +# 1->2->3 +# p c def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + cur = self.head + prev = None + # head case + if cur and cur.data == key: + self.head = cur.next + return + + # otherwise find node to remove + while cur and cur.data != key: + prev = cur + cur = cur.next + + # node to remove was not found + if cur is None: + return + + # found, remove it + prev.next = cur.next +