不用递归,颠倒一个栈。 def reverse_stack(stack): """ 非递归方式颠倒栈 Args: stack: 待颠倒的栈 Returns: 颠倒后的栈 """ # 创建一个空队列 queue = [] # 将栈中的元素依次出栈并入队 while len(stack) > 0: queue.append(stack.pop()) # 将队列中的元素依次出队并入栈 while len(queue) > 0: stack.append(queue.pop()) # 返回颠倒后的栈 return stack[::-1]