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
22 changes: 22 additions & 0 deletions 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)
# pop: O(1)
# peek: O(1)
# size: O(1)
# show: O(n)
# Space Complexity : Space Complexity is O(n)
# Did this code successfully run on Leetcode : Solved minstack successfully, The minStack needs to be traversed with the help of
# Any problem you faced while coding this : None.
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):
self.stack = []

def isEmpty(self):
return len(self.stack) == 0

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

def pop(self):
if self.isEmpty():
return print("Stack Empty")
return self.stack.pop()


def peek(self):
if self.isEmpty():
return print("Stack Empty")
return self.stack[-1]


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

def show(self):
return self.stack


s = myStack()
Expand Down
15 changes: 14 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@

# Time Complexity :
# push: O(1)
# pop: O(1)
# Space Complexity : O(n)
# Any problem you faced while coding this : None
class Node:
def __init__(self, data):
self.data = data
self.next = None

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

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

def pop(self):
if self.top is None:
return None
res = self.top.data
self.top = self.top.next
return res

a_stack = Stack()
while True:
Expand Down
27 changes: 27 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Time Complexity :
# Space Complexity :
# Did this code successfully run on Leetcode :
# Any problem you faced while coding this :
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 +23,37 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
New_Node = ListNode(data)
curr = self.head
while curr.next:
curr = curr.next

curr.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.
"""
curr = self.head

while curr.next:
if curr.data == key:
return curr
curr = curr.next
return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""

curr = self.head

while curr.next and curr.next.next:
if curr.next.val == key:
curr.next = curr.next.next
break
curr = curr.next