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
20 changes: 17 additions & 3 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
# // Time Complexity : O(n)
# // Space Complexity : O(n)
# // Did this code successfully run on Leetcode :
# // Any problem you faced while coding this :
# used stack.push rather than stack.append. append is used to push to array in python

# // Your code here along with comments explaining your approach

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

return self.stack.pop(-1)
def peek(self):
return self.stack[-1]

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

def show(self):
return self.stack


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
print(s.show())
60 changes: 59 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,74 @@

# // Time Complexity : O(1)
# // Space Complexity : O(n)
# // Did this code successfully run on Leetcode :
# // Any problem you faced while coding this :
# did it in O(n) first but can be done in O(1) time. add at begining of linked list, always add to front of list - thats top of stack

# // Your code here along with comments explaining your approach


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

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

def push(self, data):

newNode = Node(data)
if not self.head:
self.head = newNode
else:
newNode.next = self.head
self.head = newNode

def pop(self):
if self.head is None:
return None
poppedNode = self.head
self.head = self.head.next
return poppedNode.data



# this is O(n) but can be done in O(1) above
# class Node:
# def __init__(self, data):
# self.data = data
# self.next = None

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

# def push(self, data):
# newNode = Node(data)
# if self.head == None:
# self.head = newNode
# else:
# cur = self.head
# while cur.next:
# cur = cur.next

# cur.next = newNode

# def pop(self):
# cur = self.head

# while cur.next.next:
# cur = cur.next

# poppedNode = cur.next
# cur.next = None
# return poppedNode.data


# 1 -> 2 -> 3
# c c

a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
Expand Down
51 changes: 51 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@

# // Time Complexity : O(n)
# // Space Complexity : O(n)
# // Did this code successfully run on Leetcode :
# // Any problem you faced while coding this :
# - forgot the else condition in append method
# - for remove, you need to make sure you can handle the head node or last node being the node to delete, and also account for node not being found


# // Your code here along with comments explaining your approach


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 +31,53 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
if self.head is None:
self.head = ListNode(data)
else:
cur = self.head
while cur.next:
cur = cur.next

cur.next = ListNode(data)


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.
"""
cur = self.head
while cur:
if cur.data == key:
return cur
cur = cur.next
return None

# 1->2->3
# p c

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

# otherwise find node to remove
while cur and cur.data != key:
prev = cur
cur = cur.next

# node to remove was not found
if cur is None:
return

# found, remove it
prev.next = cur.next