We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I am quite new to coding so I just used the knowledge on linked lists i gained from the previous exercise. This passed all the tests but could someone help me diagnose the complexity of my solution:
# Enter your code here. Read input from STDIN. Print output to STDOUTclassLinkedListNode():data:int=Nonenext:'LinkedListNode'=NoneclassQueue():def__init__(self):self.head=Noneself.tail=Nonedefenqueue(self,x):final_element=LinkedListNode()final_element.data=x# Handle empty queue caseifnotself.head:self.head=final_elementifnotself.tail:self.tail=final_elementelse:self.tail.next=final_elementself.tail=self.tail.nextdefdequeue(self):# Handle empty queue caseifnotself.head:returnNonedequeue_value=self.head.dataself.head=self.head.next# If queue is empty, remove tailifself.headisNone:self.tail=Nonereturndequeue_valuedefhandle_query(self,query):query_type=query[0]# Enqueue queryifquery_type==1:x=query[1]self.enqueue(x)# Dequeue queryelifquery_type==2:returnself.dequeue()# Print queryelifquery_type==3:ifself.head.data:print(self.head.data)else:print(None)if__name__=="__main__":q=int(input())queue=Queue()for_inrange(q):query=list(map(int,input().split()))queue.handle_query(query)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queue using Two Stacks
You are viewing a single comment's thread. Return to all comments →
I am quite new to coding so I just used the knowledge on linked lists i gained from the previous exercise. This passed all the tests but could someone help me diagnose the complexity of my solution: