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
17 changes: 16 additions & 1 deletion Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
#// Time Complexity : O(1)
#// Space Complexity : O(n)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No

def __init__(self):
self.items = []

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

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

def pop(self):

if not self.isEmpty():
return self.items.pop()
else:
return "List is empty"

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

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

def show(self):
return self.items


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

#// Time Complexity : O(1)
#// Space Complexity : O(n)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No

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

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

def push(self, data):
new_node = Node(data)
new_node.next = self.root
self.root = new_node

def pop(self):
if self.root == None:
return None
node_popped = self.root
self.root = self.root.next
return node_popped.data

a_stack = Stack()
while True:
Expand All @@ -29,4 +42,4 @@ def pop(self):
else:
print('Popped value: ', int(popped))
elif operation == 'quit':
break
break
38 changes: 38 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#// Time Complexity : O(n)
#// Space Complexity : O(1)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No
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,48 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
new_node = ListNode(data)
if not self.head:
self.head = new_node
return

# Traverse to the end
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:
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
prev = None

while curr:
if curr.data == key:
if prev:
#skip current node
prev.next = curr.next
else:
#remove the head
self.head = curr.next
return True
prev = curr
curr = curr.next
return False