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
33 changes: 28 additions & 5 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
# Time Complexity : isEmpty: O(1), push: O(1), pop: O(n) due to shifting elements, peek: O(1)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : yes
# 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):
self.items = []
self.count = 0

def isEmpty(self):
return self.count == 0

def push(self, item):
self.items.append(item)
self.count += 1

def pop(self):

if self.isEmpty():
return None
self.count -= 1
item = self.items[self.count]
self.items.remove(item)
return item

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

def size(self):
return self.count

def show(self):
return self.items


s = myStack()
s.push('1')
s.push('2')
print(s.pop())
print(s.show())
s.push('3')
print("List: ", s.show(), "Size: ", s.size())
print("Pop: ", s.pop())
print("List: ", s.show(), "Size: ", s.size())
s.push('4')
print("Peek: ", s.peek())
print("List: ", s.show(), "Size: ", s.size())
18 changes: 17 additions & 1 deletion Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Time Complexity : push: O(1), pop: O(1)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : understanding it's better to push new nodes at the head instead of tail of LL.

class Node:
def __init__(self, data):
Expand All @@ -6,10 +10,22 @@ def __init__(self, data):

class Stack:
def __init__(self):

self.head = None
self.count = 0

def push(self, data):
pushNode = Node(data)
pushNode.next = self.head
self.head = pushNode
self.count += 1

def pop(self):
if self.count == 0:
return None
popped = self.head.data
self.head = self.head.next
self.count -= 1
return popped

a_stack = Stack()
while True:
Expand Down
86 changes: 86 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# Did this code successfully run on Leetcode : yes
# Any problem you faced while coding this : no, but remove() was made much easier by visualizing the LL.

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

"""
Display the node for debugging.
"""
def display(self):
if self.data is not None:
print("node data: ", self.data)
else:
print("empty node")

class SinglyLinkedList:
def __init__(self):
Expand All @@ -17,16 +31,88 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
newNode = ListNode(data, None)
if self.head is None:
self.head = newNode
return
lastNode = self.head
while lastNode.next is not None:
lastNode = lastNode.next
lastNode.next = newNode

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.
"""
current = self.head
while current is not None:
if current.data == key:
return current
current = current.next
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 False
if self.head.data == key:
self.head = self.head.next
return True
current = self.head
while current.next is not None:
if current.next.data == key:
current.next = current.next.next
return True
current = current.next
return False

def display(self):
"""
Display the linked list contents for debugging.
"""
if self.head is None:
print("List is empty")
return

elements = []
current = self.head
while current is not None:
elements.append(str(current.data))
current = current.next
print("List:", " -> ".join(elements))


# Debugging
ll = SinglyLinkedList()

ll.append(1)
ll.append(2)
ll.append(3)
ll.display()

print("\nFinding element 2:")
found = ll.find(2)
found.display()

print("\nRemoving element 2:")
removed = ll.remove(2)
ll.display()

# Test find after removal
print("\nFinding element 2 after removal:")
found = ll.find(2)
if found:
found.display()
else:
print("not found")

# Test remove non-existent element
print("\nTrying to remove element 5 (doesn't exist):")
removed = ll.remove(5)
print(removed)
ll.display()