I was asked on the best and worst case scenario for this function:
def is_palindromic(the_list):
result = True
the_stack=Stack()
for item in the_list:
the_stack.push(item)
for item in the_list:
item_stack=the_stack.pop()
if item != item_stack:
result = False
return result
This function determines if a list is the same as its reverse using a stack. I thought the time complexity was the same for every case but when I tested, it took longer to run if the list was indeed the same as its reverse. Can anyone explain why? I am a bit confused.