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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
20 changes: 14 additions & 6 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
#Time Complexity: push: O(1), pop: O(1), peek: O(1), isEmpty: O(1), size: O(1), show: O(n)
#Space Complexity: O(n)

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()

def peek(self):

if not self.isEmpty():
return self.items[-1]

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

def show(self):

print(self.items)

s = myStack()
s.push('1')
Expand Down
13 changes: 13 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@

class Node:
#TIME: push: O(1), pop: O(1), __init__ : O(1)
#SPACE: O(n)
def __init__(self, data):
self.data = data
self.next = None

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

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

def pop(self):
if self.isEmpty():
return None
popped_node = self.top
self.top = self.top.next
return popped_node.data



a_stack = Stack()
while True:
Expand Down
47 changes: 47 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class ListNode:
#TIME: append: O(n), find: O(n), remove: O(n)
#SPACE: O(1)
"""
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 @@ -13,20 +17,63 @@ def __init__(self):
self.head = None

def append(self, data):
new_node = ListNode(data)
# If list is empty
if self.head is None:
self.head = new_node
return
# Traverse to the end of the list
current = self.head
while current.next:
current = current.next
current.next = new_node

"""
Insert a new element at the end of the list.
Takes O(n) time.
"""

def find(self, key):
current = self.head
while current:
if current.data == key:
return current
current = current.next
return None
"""
Search for the first element with `data` matching
`key`. Return the element or `None` if not found.
Takes O(n) time.
"""

def remove(self, key):
current = self.head
previous = None

#if head contains the key
if current and current.data == key:
self.head = current.next
return
#search for the key to be deleted
while current and current.data != key:
previous = current
current = current.next
#if key was not present in linked list
if current is None:
return
#unlink the node from linked list
previous.next = current.next
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""



ll = SinglyLinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
print(ll.find(20).data) # 20
ll.remove(20)
print(ll.find(20)) # None