diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..b5adac524 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,84 @@ -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): + +#Did this code successfully run on Leetcode : +#Any problem you faced while coding this : + +#Your code here along with comments explaining your approach + +#Exercise_1 : Implement Stack using Array. +# class myStack: +# #initializing the stack and top variable +# def __init__(self): +# self.stack = [] +# self.top = -1 - def push(self, item): +# def isEmpty(self): +# return self.top == -1 + +# def push(self, item): +# self.top += 1 +# self.stack.append(item) - def pop(self): +# def pop(self): +# if self.top == -1: +# print("stack is empty") +# return None +# self.top -= 1 +# return self.stack.pop() - - def peek(self): - - def size(self): - - def show(self): - +# def peek(self): +# return self.stack[self.top] + +# def size(self): +# count = 0 +# for i in range(self.top, -1, -1): +# if self.top == -1: +# count = 0 +# else: +# count += 1 +# return count + +# def show(self): +# for i in range(self.top, -1, -1): +# print(self.stack[i]) + +#Solution 2 + +class myStack: + def __init__(self): + self.stack = [] + self.top = -1 + + def isEmpty(self): + return len(self.stack) == 0 + + def push(self, item): + self.stack.append(item) + + def pop(self): + if self.isEmpty(): + raise IndexError("Stack is emtpy nothing to pop") + return self.stack.pop() + + def peek(self): + if self.isEmpty(): + raise IndexError("Stack is empty no peek element") + print(self.stack[-1]) + return self.stack[-1] + + def size(self): + return len(self.stack) + + def show(self): + print(self.stack) s = myStack() s.push('1') s.push('2') -print(s.pop()) -print(s.show()) +s.pop() +s.peek() +s.show() + +#Time Complexity : O(1) for all functions. show() O(n) +#Space Complexity :O(n) size of stack + + diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..05fb2c566 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,6 @@ +# Exercise_2 : Implement Stack using Linked List. + class Node: def __init__(self, data): self.data = data @@ -6,10 +8,25 @@ def __init__(self, data): class Stack: def __init__(self): + self.top = None def push(self, data): + newNode = Node(data) + + newNode.next = self.top + self.top = newNode def pop(self): + if self.top is None: + return None + + popped_value = self.top.data + self.top = self.top.next + return popped_value + +#Time Complexity : O(1) +#Space complexity : O(n) + a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..234be4c41 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,12 @@ +#Exercise_3 : Implement Singly Linked List. + 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 +21,17 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + + newNode = ListNode(data) + if self.head is None: + self.head = newNode + return + + current = self.head + while current.next: + current = current.next + + current.next = newNode def find(self, key): """ @@ -24,9 +39,43 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + + if self.head is None: + return + + current = self.head + while current.next : + if current.data == key: + return current + current = current.next + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + + if self.head is None: + return None + + current = self.head + if current.data == key: + current = current.next + return + + while current.next : + if current.next.data == key: + current.next = current.next.next + return + current = current.next + + +linked_list = SinglyLinkedList() + +linked_list.append(10) +linked_list.append(20) +linked_list.append(30) + +#TimeComplexity : O(n) for all functions +#Space Complexity :O(n) \ No newline at end of file