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
36 changes: 28 additions & 8 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
'''
Time Complexity : O(1)
Space Complexity : O(1)
Did this code successfully run on Leetcode : Yes, but the question on leetcode was using Queues and not Arrays
Any problem you faced while coding this : No

'''

class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
def __init__(self):
self.storage = []

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

def push(self, item):

def pop(self):

def push(self, item):
self.storage.append(item)

def pop(self):
if not self.isEmpty():
self.storage.pop()

def peek(self):

def size(self):
def peek(self):
if not self.isEmpty():
return self.storage[-1]

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

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


s = myStack()
Expand Down
9 changes: 9 additions & 0 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ 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
temp = self.head
self.head = self.head.next
return temp.data

a_stack = Stack()
while True:
Expand Down
34 changes: 34 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class ListNode:
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = None

class SinglyLinkedList:
def __init__(self):
Expand All @@ -17,16 +19,48 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
new_node = ListNode(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next != None:
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.
"""
if self.head is None:
return None
else:
current = self.head
while(current.data != key and current.next != None):
current = current.next
if current.data == key:
return current.data
return None

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