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
10 changes: 9 additions & 1 deletion Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
# Time complexity: 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):
self.q = deque() #initializing an empty deque

def isEmpty(self):
return len(self.q) == 0 #returns true when stack is empty

def push(self, item):
self.q.append(item) #adds a value to the end which is the top of the stack

def pop(self):

return self.q.pop() #removes last value that is inserted

def peek(self):
return self.q[-1] #returns the last(top most) value

def size(self):
return len(self.q) # returns no.of elements in the stack

def show(self):
return list(self.q) #return all elements in the stack


s = myStack()
Expand Down
38 changes: 34 additions & 4 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
# Time Complexity : O(1)
# Space Complexity : O(n)
# O(n)

# Did this code successfully run on Leetcode : No, I did not solve this problem in leetcode

# Any problem you faced while coding this : Took sometime to write the code by myself but understood by taking help

class Node:
def __init__(self, data):
self.data = data
self.next = None
self.data = data
self.next = None


class Stack:
def __init__(self):
self.head = None # top of stack

def push(self, data):
# create new node
new_node = Node(data)

# point new node to current head
new_node.next = self.head

# update head to new node
self.head = new_node

def pop(self):
# check if stack is empty
if self.head is None:
return None

# store current head data
popped = self.head.data

# move head to next node
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"
print('push <value>')
print('pop')
print('quit')

do = input('What would you like to do? ').split()
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
operation = do[0].strip().lower()

if operation == 'push':
a_stack.push(int(do[1]))

elif operation == 'pop':
popped = a_stack.pop()
if popped is None:
print('Stack is empty.')
else:
print('Popped value: ', int(popped))

elif operation == 'quit':
break
51 changes: 49 additions & 2 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
# Time Complexity : append -> O(n), find -> O(n),remove -> O(n)

# Space Complexity : O(n)

# Did this code successfully run on Leetcode : Did not solve it on leet code, but I tested it on online compiler

# Any problem you faced while coding this : Felt it bit tricky but finally understood and I'm poor at handling edge cases


class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):

self.data = data # store value in node
self.next = next # pointer to next node


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

def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
new_node = ListNode(data) # create new node

if self.head is None: # if list is empty
self.head = new_node # make new node as head
return

temp = self.head # start from head

while temp.next: # traverse till last node
temp = temp.next

temp.next = new_node # attach new node at end

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.
"""
temp = self.head # start from head

while temp: # traverse list
if temp.data == key: # check if value matches
return temp # return node if found
temp = temp.next

return None # return None if not found

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
temp = self.head # start from head
prev = None # keep track of previous node

while temp:
if temp.data == key: # if value matches

if prev is None: # if node to delete is head
self.head = temp.next # move head forward
else:
prev.next = temp.next # bypass current node

return # exit after deletion

prev = temp # move prev forward
temp = temp.next # move temp forward