diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..0dbd8a506 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,36 @@ +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file def __init__(self): - + self.s = [] + self.i = -1 + def isEmpty(self): + return len(self.s) == 0 def push(self, item): + self.s.append(item) + self.i += 1 def pop(self): - + if self.i >= 0: + a = self.s.pop() + self.i -= 1 + return a def peek(self): - + return self.s[self.i] + def size(self): + return len(self.s) def show(self): + for e in self.s: + print(e) s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..6621d0bd9 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,7 @@ - +# Time Complexity : O(1) +# Space Complexity : O(n) +# Did this code successfully run on Leetcode : Yes +# Any problem you faced while coding this : class Node: def __init__(self, data): self.data = data @@ -6,10 +9,35 @@ def __init__(self, data): class Stack: def __init__(self): + self.head = Node(None) + self.tail = self.head + self.count = 0 def push(self, data): + newNode = Node(data) + if self.tail == self.head: + self.head.next = newNode + else: + self.tail.next = newNode + + self.tail = newNode + self.count += 1 + def pop(self): + if self.count > 0: + data = self.tail.data + temp = self.head + while temp.next != self.tail: + temp = temp.next + + self.tail = temp + self.tail.next = None + self.count -= 1 + + return data + else: + return None a_stack = Stack() while True: diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..7fdf52220 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 + self.next = next class SinglyLinkedList: def __init__(self): @@ -10,13 +12,20 @@ def __init__(self): Create a new singly-linked list. Takes O(1) time. """ - self.head = None + self.head = ListNode() def append(self, data): """ Insert a new element at the end of the list. Takes O(n) time. """ + newNode = ListNode(data) + temp = self.head + while temp.next != None: + temp = temp.next + + temp.next = newNode + def find(self, key): """ @@ -24,9 +33,43 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + temp = self.head + + while temp.next: + if temp.next.data == key: + return temp.next + else: + temp = temp.next + return None + + def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + previous = self.head + + while previous.next: + if previous.next.data == key: + break + else: + previous = previous.next + + # if we didn't find the key, previous will be TAIL, and previous.next will be None + # else previous.next will be the item to remove + if previous.next: + itemToRemove = previous.next + previous.next = itemToRemove.next + itemToRemove.next = None + + +ll = SinglyLinkedList() +ll.append(1) +ll.append(2) +ll.append(3) +print((ll.find(2)).data) +print(ll.find(5)) +ll.remove(2) +print(ll.find(2)) \ No newline at end of file