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
40 changes: 27 additions & 13 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
# Time Complexity : Push: O(1), POP: O(1), Peek: O(1), size: O(1)

# 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):

def isEmpty(self):

def push(self, item):

def pop(self):


def peek(self):
def __init__(self):
self.arr = []

def isEmpty(self):
if len(self.arr) == 0:
return True
return False

def push(self, item):
self.arr.append(item)
print(self.arr)

def pop(self):
return self.arr.pop()

def peek(self):
if self.arr:
return self.arr[-1]

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

def size(self):

def show(self):
def show(self):
return self.arr


s = myStack()
Expand Down
10 changes: 10 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# Time Complexity : push: O(1)

# Space Complexity : pop: O(1)
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Stack:
def __init__(self):
self.head = Node(-1)

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

def pop(self):
curr = self.head.next
if curr:
self.head.next = curr.next
return curr.data if curr else None

a_stack = Stack()
while True:
Expand Down
21 changes: 20 additions & 1 deletion Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,49 @@ class ListNode:
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.val = data

class SinglyLinkedList:
def __init__(self):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.head = None
self.head = ListNode(-1)

def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
curr = self.head
node = ListNode(data)
while curr.next != None:
curr = curr.next
curr.next = 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.
"""
curr = self.head.next
while curr:
if curr.data == key:
return curr
return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
curr = self.head.next
prev = None
while curr and curr.data != key:
prev = curr
curr = curr.next
if prev:
prev.next = curr.next
return