Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
# Time Complexity:
# isEmpty: O(1)
# push: O(1) amortized
# pop: O(1)
# peek: O(1)
# size: O(1)
# show: O(n)

# Space Complexity: O(n)

class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
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 None

def peek(self):
if not self.isEmpty():
return self.items[-1]
else:
return None

def size(self):
return len(self.items)

def show(self):
return self.items


s = myStack()
Expand Down
17 changes: 16 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Time Complexity:
# push: O(1)
# pop: O(1)

# Space Complexity: O(n)

class Node:
def __init__(self, data):
Expand All @@ -6,11 +11,21 @@ def __init__(self, data):

class Stack:
def __init__(self):
self.head = None

def push(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node

def pop(self):

if self.head is None:
return None
else:
popped = self.head.data
self.head = self.head.next
return popped

a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
Expand Down
87 changes: 85 additions & 2 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Time Complexity:
# append: O(n)
# find: O(n)
# remove: O(n)

# Space Complexity:
# O(n) for storing n nodes
# O(1) extra space for each operation

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):
Expand All @@ -17,16 +28,88 @@ 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
else:
current = self.head
while current.next:
current = current.next
current.next = new_node

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.
"""

current = self.head
while current:
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.
"""
current = self.head
previous = None
while current:
if current.data == key:
if previous:
previous.next = current.next
else:
self.head = current.next
current.next = None
return current
previous = current
current = current.next
return None

if __name__ == "__main__":
linked_list = SinglyLinkedList()

print("Empty list tests:")
print(linked_list.find(10))
print(linked_list.remove(10))
linked_list.append(10)
linked_list.append(20)
linked_list.append(30)
linked_list.append(40)

print("\nFind tests:")

found = linked_list.find(20)
print(found.data if found else None)

found = linked_list.find(40)
print(found.data if found else None)

found = linked_list.find(99)
print(found.data if found else None)

linked_list.remove(10)
print("\nAfter removing head 10:")
print(linked_list.head.data)

linked_list.remove(30)
print("\nAfter removing middle value 30:")
print(linked_list.find(30))

linked_list.remove(40)
print("\nAfter removing tail 40:")
print(linked_list.find(40))

print("\nRemaining head:")
print(linked_list.head.data)

linked_list.remove(99)
print("\nAfter trying to remove 99:")
print(linked_list.head.data)

linked_list.remove(20)
print("\nAfter removing final node:")
print(linked_list.head)