geeksforgeeks
search icon
  • Courses
    Sale
  • Tutorials
  • Interview Prep
    Videos
    January 10, 2025 |57.9K Views

    Reverse a stack using recursion

    stack, Stack
     Save  Share  1 Like
    Description
    Discussion

    Write a program to reverse a stack using recursion. You are not allowed to use loop constructs like while, for..etc, and you can only use the following ADT functions on Stack S: 
    isEmpty(S) 
    push(S) 
    pop(S)

    The idea of the solution is to hold all values in Function Call Stack until the stack becomes empty. When the stack becomes empty, insert all held items one by one at the bottom of the stack. 
    For example, let the input stack be  

    Reverse a stack using recursion: https://www.geeksforgeeks.org/reverse-a-stack-using-recursion/

    Recommendations
    Video Thumbnail
    02:38
    Video Thumbnail
    31.4K Views | 10/01/2025...
    Design and Implement Special Stack Data Structure
    Video Thumbnail
    06:43
    Video Thumbnail
    26.2K Views | 10/01/2025...
    Iterative Preorder Traversal
    Video Thumbnail
    27:18
    Video Thumbnail
    79.8K Views | 09/01/2025...
    The Stock Span Problem
    Video Thumbnail
    04:59
    Video Thumbnail
    14.7K Views | 08/01/2025...
    Check if a given array can represent Preorder Traversal of Binary Search Tree
    Video Thumbnail
    10:24
    Video Thumbnail
    78.3K Views | 04/01/2025...
    Stack | Set 4 (Evaluation of Postfix Expression)
    Video Thumbnail
    06:30
    Video Thumbnail
    16.1K Views | 23/08/2024...
    WHAT IS STACK? | Stack Data Structures | DSA Course
    Video Thumbnail
    12:08
    Video Thumbnail
    13.7K Views | 08/07/2022...
    K stacks in a single array
    Video Thumbnail
    11:51
    Video Thumbnail
    18.8K Views | 08/07/2022...
    Design a stack with operations on middle element
    Video Thumbnail
    02:41
    Video Thumbnail
    7.0K Views | 07/07/2022...
    Interleave the first half of the queue with second half
    Video Thumbnail
    05:07
    Video Thumbnail
    8.1K Views | 04/07/2022...
    Check if a queue can be sorted in another queue using a stack
    GeeksforGeeks
    location
    Corporate & Communications Address:
    A-143, 6th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
    location
    Registered Address:
    K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
    GFG App on Play StoreGFG App on App Store
    • Company
    • About Us
    • Legal
    • Privacy Policy
    • Contact Us
    • Advertise with us
    • GFG Corporate Solution
    • Campus Training Program
    • Explore
    • POTD
    • Job-A-Thon
    • Blogs
    • Nation Skill Up
    • Tutorials
    • Programming Languages
    • DSA
    • Web Technology
    • AI, ML & Data Science
    • DevOps
    • CS Core Subjects
    • Interview Preparation
    • Software and Tools
    • Courses
    • ML and Data Science
    • DSA and Placements
    • Web Development
    • Programming Languages
    • DevOps & Cloud
    • GATE
    • Trending Technologies
    • Videos
    • DSA
    • Python
    • Java
    • C++
    • Web Development
    • Data Science
    • CS Subjects
    • Preparation Corner
    • Interview Corner
    • Aptitude
    • Puzzles
    • GfG 160
    • System Design
    @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved