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
41 changes: 30 additions & 11 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
# Time Complexity : O(1) ; All stack operation perform in constant time
# Space Complexity : O(n); n = number of elements in the stack
# Did this code successfully run on Leetcode : -
# Any problem you faced while coding this : No

# 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):
def __init__(self):
self.stack=[]

def isEmpty(self):
def isEmpty(self): #checking if stack is Empty
return False if self.stack else True

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

def pop(self):
def pop(self):
if self.isEmpty():
print("Stack is empty; Cannot pop")
else:
return self.stack.pop() #remove and return the top element

def peek(self):
if self.isEmpty():
print("Stack is empty; Cannot peek")
else:
return self.stack[-1]

def peek(self):

def size(self):

def show(self):
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())


20 changes: 17 additions & 3 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
# Time Complexity : O(1) ; All stack operation perform in constant time
# Space Complexity : O(n); n = number of elements in the stack
# Did this code successfully run on Leetcode : -
# Any problem you faced while coding this : No

# 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)
newnode.next = self.head
self.head = newnode

def pop(self):

if self.head:
x = self.head.data
self.head = self.head.next
return x


a_stack = Stack()
while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
Expand Down
69 changes: 68 additions & 1 deletion Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
'''Time Complexity : O(n) ; n = number of elements in the LinkedList
To insert, remove or find key, we need to traverse entire linklist
Space Complexity : O(n);
Did this code successfully run on Leetcode : -
Any problem you faced while coding this : No

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 = None

class SinglyLinkedList:
def __init__(self):
Expand All @@ -17,16 +28,72 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""

newnode = ListNode(data)
if not self.head: #if Linklist is empty, make new node as first/head node
self.head=newnode
else: #if linkList has nodes present, append newnode at end of list
temp = self.head
while temp.next:
temp = temp.next
temp.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.
"""
temp = self.head
while temp.data != key:
temp = temp.next
print("Key Found :",temp.data)

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
if self.head.data == key: # if key is at start node
x = self.head.data
self.head = self.head.next
else:
temp = self.head
while temp.data != key:
prev = temp
temp = temp.next
x = temp.data
if temp.next is None: #if key is at last node
prev.next = None
else: #if key is at middle node
prev.next = temp.next
print("Remove key:",x)


def show(self):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
l = []
temp = self.head
while temp:
l.append(temp.data)
temp = temp.next
print("Linked List : ", l)


s = SinglyLinkedList()
s.append(1)
s.append(2)
s.append(3)
s.show()
s.find(2)
s.remove(1) #remove 1st node
s.show()
s.append(4)
s.append(5)
s.show()
s.remove(5) #ewmove last node
s.show()
s.remove(3) #remove middle node
s.show()