Reverse a stack using recursion

Reverse a stack using recursion

Solution:
  • The main part of this is add an element at bottom of stack
  • That can be done by pop all stack element in temporary variable till stack not beacome empty
  • This call will be through recursion
Algorithm
  • Pop an element from stack
  • Recursively call this function till stack not become empty
  • Now call insertAtBottom function with popped element
InsertAtBottom Function
  • If stack is empty then
    • Add given element to stack
    • And return from function
  • Pop element from stack
  • And recursively call InsertAtBottom
  • Add pop element back to stack

Latest Source Code:
Github: StackReverse.java


Output:

1 2 3 4 5 6 7 8 9 
101 102 4 5 6 7 8 9 
Y node : Node(4)

Author: Hrishikesh Mishra