diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..18686cb8e 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,20 +1,35 @@ class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file + #// Time Complexity : O(1) + #// Space Complexity : O(n) + #// Did this code successfully run on Leetcode : Yes + #// Any problem you faced while coding this : No + def __init__(self): + self.items = [] def isEmpty(self): + return len(self.items) == 0 def push(self, item): + self.items.append(item) def pop(self): - + if not self.isEmpty(): + return self.items.pop() + else: + return "List is empty" def peek(self): + if not self.isEmpty(): + return self.items[-1] def size(self): + return len(self.items) def show(self): + return self.items s = myStack() diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..6f3ad2448 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,8 @@ - +#// Time Complexity : O(1) +#// Space Complexity : O(n) +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : No + class Node: def __init__(self, data): self.data = data @@ -6,10 +10,19 @@ def __init__(self, data): class Stack: def __init__(self): + self.root = None def push(self, data): + new_node = Node(data) + new_node.next = self.root + self.root = new_node def pop(self): + if self.root == None: + return None + node_popped = self.root + self.root = self.root.next + return node_popped.data a_stack = Stack() while True: @@ -29,4 +42,4 @@ def pop(self): else: print('Popped value: ', int(popped)) elif operation == 'quit': - break + break \ No newline at end of file diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..01d0d7997 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -1,8 +1,14 @@ +#// Time Complexity : O(n) +#// Space Complexity : O(1) +#// Did this code successfully run on Leetcode : Yes +#// Any problem you faced while coding this : No 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 +23,17 @@ def append(self, data): Insert a new element at the end of the list. Takes O(n) time. """ + new_node = ListNode(data) + if not self.head: + self.head = new_node + return + + # Traverse to the end + curr = self.head + while curr.next: + curr = curr.next + curr.next = new_node + def find(self, key): """ @@ -24,9 +41,30 @@ def find(self, key): `key`. Return the element or `None` if not found. Takes O(n) time. """ + curr = self.head + while curr: + if curr.data == key: + return curr + curr = curr.next + return None def remove(self, key): """ Remove the first occurrence of `key` in the list. Takes O(n) time. """ + curr = self.head + prev = None + + while curr: + if curr.data == key: + if prev: + #skip current node + prev.next = curr.next + else: + #remove the head + self.head = curr.next + return True + prev = curr + curr = curr.next + return False \ No newline at end of file