diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..9784b537c 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,24 +1,43 @@ +# Time Complexity : O(1) ; All stack operation perform in constant time +# Space Complexity : O(n); n = number of elements in the stack +# Did this code successfully run on Leetcode : - +# Any problem you faced while coding this : No + +# 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): + def __init__(self): + self.stack=[] - def isEmpty(self): + def isEmpty(self): #checking if stack is Empty + return False if self.stack else True - def push(self, item): + def push(self, item): + self.stack.append(item) - def pop(self): + def pop(self): + if self.isEmpty(): + print("Stack is empty; Cannot pop") + else: + return self.stack.pop() #remove and return the top element + def peek(self): + if self.isEmpty(): + print("Stack is empty; Cannot peek") + else: + return self.stack[-1] - def peek(self): - - def size(self): - - def show(self): + 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()) + + diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..74b241dbb 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,9 @@ +# Time Complexity : O(1) ; All stack operation perform in constant time +# Space Complexity : O(n); n = number of elements in the stack +# Did this code successfully run on Leetcode : - +# Any problem you faced while coding this : No +# Your code here along with comments explaining your approach class Node: def __init__(self, data): self.data = data @@ -6,11 +11,20 @@ def __init__(self, data): class Stack: def __init__(self): - + self.head = None + def push(self, data): - + newnode = Node(data) + newnode.next = self.head + self.head = newnode + def pop(self): - + if self.head: + x = self.head.data + self.head = self.head.next + return x + + 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..3a3fce6bb 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,19 @@ +'''Time Complexity : O(n) ; n = number of elements in the LinkedList + To insert, remove or find key, we need to traverse entire linklist +Space Complexity : O(n); +Did this code successfully run on Leetcode : - +Any problem you faced while coding this : No + +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 = None class SinglyLinkedList: def __init__(self): @@ -17,16 +28,72 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ - + newnode = ListNode(data) + if not self.head: #if Linklist is empty, make new node as first/head node + self.head=newnode + else: #if linkList has nodes present, append newnode at end of list + temp = self.head + while temp.next: + temp = temp.next + temp.next = newnode + def find(self, key): """ Search for the first element with `data` matching `key`. Return the element or `None` if not found. Takes O(n) time. """ + temp = self.head + while temp.data != key: + temp = temp.next + print("Key Found :",temp.data) def remove(self, key): + """ + Remove the first occurrence of `key` in the list. + Takes O(n) time. + """ + if self.head.data == key: # if key is at start node + x = self.head.data + self.head = self.head.next + else: + temp = self.head + while temp.data != key: + prev = temp + temp = temp.next + x = temp.data + if temp.next is None: #if key is at last node + prev.next = None + else: #if key is at middle node + prev.next = temp.next + print("Remove key:",x) + + + def show(self): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + l = [] + temp = self.head + while temp: + l.append(temp.data) + temp = temp.next + print("Linked List : ", l) + + +s = SinglyLinkedList() +s.append(1) +s.append(2) +s.append(3) +s.show() +s.find(2) +s.remove(1) #remove 1st node +s.show() +s.append(4) +s.append(5) +s.show() +s.remove(5) #ewmove last node +s.show() +s.remove(3) #remove middle node +s.show() \ No newline at end of file