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
40 changes: 33 additions & 7 deletions Exercise_1.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@

# Time Complexity : O(1)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes on Geeksforgeeks
# 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):

self.stacklist = []

# // Time Complexity : O(1)
# // Space Complexity :
def isEmpty(self):

return not self.stacklist

# // Time Complexity : O(1)
# // Space Complexity : O(n)
def push(self, item):

self.stacklist.append(item)

# // Time Complexity : O(1)
# // Space Complexity :
def pop(self):


return self.stacklist.pop()

# // Time Complexity : O(1)
# // Space Complexity :
def peek(self):

return self.stacklist[-1]

# // Time Complexity : O(1)
# // Space Complexity :
def size(self):

return len(self.stacklist)

# // Time Complexity : O(1)
# // Space Complexity :
def show(self):
return self.stacklist


s = myStack()
Expand Down
27 changes: 24 additions & 3 deletions Exercise_2.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@

# Time Complexity : O(1)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes on Geeksforgeeks
# Any problem you faced while coding this : Faced difficulty while writing a logic for pop
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Stack:
def __init__(self):

self.top = None

def push(self, data):

new_node = Node(data)
# if there is something in the stack
if self.top:
# new_node's pointer will point to that top
new_node.next = self.top
# if there is nothing already then new node become top
self.top = new_node

def pop(self):
# if top is present
if self.top:
# popped the top and store into the popped variable
popped = self.top
# link the top to the next pointer of the popped
self.top = popped.next
return popped.data
return None


a_stack = Stack()

while True:
#Give input as string if getting an EOF error. Give input like "push 10" or "pop"
print('push <value>')
Expand Down
73 changes: 73 additions & 0 deletions Exercise_3.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,105 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : No. Could not find the problem
# Any problem you faced while coding this : Faced difficulty while writing a logic for append and find
class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
# initilize the data and next attribute
self.data = data
self.next = next

class SinglyLinkedList:
def __init__(self):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
# initializing head with None
self.head = None

def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
# creating an object
next_node = ListNode(data)
# first node will be head
# second node will linked to next of the first node
# third node will linked to next of the second node
# fourth node will linked to next of the third node
if self.head:
# line 32 to 35 will find last node in the linked list
# can not iterate on head because we don't want to change the head of the linked list
node = self.head
# iterating over the node untill we find the last node which will have null in its next
while node.next:
# incrementing the node to check the null
node = node.next
# this node is the last node in the linked list. we are linking the new node with this
node.next = next_node
else:
# first node will be the head
self.head = next_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.
"""
if self.head:
node = self.head
# iterating over the entire linked list to find the matching key
while node:
if node.data == key:
return node
node = node.next
# if there's nothing in the list and no matching element is found
return None

def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
# iterating over the entire linked list to find the matching key
if self.head:
node = self.head
previous_node = None
# iterating over the entire linked list to find the matching key
while node:
# checking key with the list data
if node.data == key:
if previous_node:
if node.next:
# we have both previous and next, so we are removing middle element and previous is pointing to next
previous_node.next = node.next
# when there is no next element the previous node will point to None
else:
previous_node.next = None
else:
self.head = node.next
previous_node = node
node = node.next
return None

s_link = SinglyLinkedList()
s_link.append(3)
s_link.append(4)
# s_link.remove(4)
# s_link.remove(3)
# print(s_link.head.next)
print(s_link.find(4))
print(s_link.find(2))
# print(s_link.head)
node = s_link.head
while node:
print(node)
node = node.next