diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5..58efc06f 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,22 +1,57 @@ +# Time Complexity : +# All the methods defined in below have time complexity O(1) except show method, show method prints the stack so it has time complexity of O(n) where n being the number of elements in the stack +# Space Complexity : +# Space complexiy for all the methods is O(1) since its constant like length, pop or peek will return just one single element. However for the show method it would be O(n) were n being the number of elements in the stack. +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : None + 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 = [] # here I am defining stack as a list def isEmpty(self): + if self.__stack: + return False + else: + return True def push(self, item): + # inserting element at the top of stack meaning end of list + self.__stack.append(item) def pop(self): + # removing last element from the list + # first check if list is empty + if not self.__stack: + # stack is empty + raise Exception("pop can not be done on empty stack") + else: + # stack is not empty + # remove the last element from the list + return self.__stack.pop() def peek(self): + # peek will return the top value from the stack without removing it + if not self.__stack: + # stack is empty + raise Exception("peek is called on empty stack") + else: + # stack has some values + # get its length + peek_index = len(self.__stack) - 1 + return self.__stack[peek_index] + def size(self): + # size of stack would be length of list + return len(self.__stack) def show(self): - - + # return the stack + return self.__stack + + s = myStack() s.push('1') s.push('2') diff --git a/Exercise_2.py b/Exercise_2.py index b1149221..7fbd75a0 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,15 +1,49 @@ +# Time Complexity : +# All the methods defined in below have time complexity O(1) except show method, show method prints the stack so it has time complexity of O(n) where n being the number of elements in the stack +# Space Complexity : +# Space complexiy for all the methods is O(1) since its constant like length, pop or peek will return just one single element. However for the show method it would be O(n) were n being the number of elements in the stack. +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : I had to revise concepts on linked list. class Node: + def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): + # stack is node, like linked list so initially it would be empty + self.top = None # Basically I am initilizaling a node named top which is None. + # top is just a variable def push(self, data): + # inserting data + # create new node + new_node = Node(data) + new_node.next = self.top # linking new node to top, so its basically (0)newNode --> top + # make the new node as top + self.top = new_node def pop(self): + # first check if stack is empty + if self.top is None: + # stack is empty + raise Exception("pop can not be done on empty stack") + else: + pop_data = self.top.data + # unlink that top mode + # plus one the pointer of stack + self.top = self.top.next + + def show(self): + # Traverse the stack and collect all values into a list + elements = [] + current = self.top + while current is not None: + elements.append(current.data) + current = current.next + return elements a_stack = Stack() while True: @@ -28,5 +62,7 @@ def pop(self): print('Stack is empty.') else: print('Popped value: ', int(popped)) + elif operation == 'show': + print(a_stack.show()) elif operation == 'quit': break diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b5..21e7e496 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,6 +3,8 @@ class ListNode: A node in a singly-linked list. """ def __init__(self, data=None, next=None): + self.data=data # The data value stored in the node + self.next=next # Reference to the next node (or None if no next node) class SinglyLinkedList: def __init__(self): @@ -10,13 +12,29 @@ def __init__(self): Create a new singly-linked list. Takes O(1) time. """ - self.head = None + self.head = None # Initially, the list is empty + def append(self, data): """ Insert a new element at the end of the list. Takes O(n) time. """ + new_node = ListNode(data) # First Create a new node with the given data + # Now place that node at the end of list + # if this is first node + if not self.head: + # this is first node of the list + self.head = new_node + else: + # link list has some values, need to iterate to the end and assign nodes + current = self.head # pointer to iterate + while(current.next): + current = current.next + + # now current is our end node + # assign new node to the end of current node + current.next = new_node def find(self, key): """ @@ -25,8 +43,58 @@ def find(self, key): Takes O(n) time. """ + # initialize the pointer which we call current + current = self.head + + while(current): + if current.data == key: + # key found + return current.data + else: + # increment current + current = current.next + + # if we are outside while loop that means, list is either empty or key is not present in the list + # in this case return None + return None + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + current = self.head + + # If the list is empty, return False + if not current: + return False + + # Special case: If the head node is the one to be removed + if current.data == key: + self.head = current.next # Move the head pointer to the next node + return True + + left = ListNode() + # Traverse the list to find the node to remove + while current: + if current.data == key: + left.next = current.next # Bypass the node to be removed + return True + left = current # move left + current = current.next # increment current + + # Return False if the key was not found + return False + + + def display(self): + """ + Helper method to display the list as a string (for testing). + """ + elements = [] + current = self.head + while current: + elements.append(current.data) + current = current.next + return "->".join(map(str, elements))