Cloning a Stack without Using Extra Memory Using Java

Last Updated : 18 May 2026

We have given a source stack st[]. The task is to copy the contents of the source stack st[] to the destination stack cloned[] such that the order of elements remains the same without using extra space.

To read more Stack Data Structure

Example 1:

Input: Stack = [1, 2, 3]

Output: Cloned Stack = [1, 2, 3]

Explanation: The top element (3) is removed first, followed by 2 and 1 recursively. Elements are added at the bottom of the cloned stack in the proper sequence when going back. Simultaneous restoration of the original stack ensures that the elements in both stacks are the same.

Example 2:

Input: Stack = [10, 20, 30, 40]

Output: Cloned Stack = [10, 20, 30, 40]

Explanation: Every element is taken out and put back in recursively. The order is maintained due to the insert-at-bottom procedure. By temporarily storing elements, the recursion stack avoids the need for additional memory structures.

Example 3:

Input: Stack = [5]

Output: Cloned Stack = [5]

Explanation: The recursion quickly reaches the base condition when there is only one element. Identical stacks are created by inserting the element into the cloned stack and then restoring it.

Example 4:

Input: Stack = []

Output: Cloned Stack = []

Explanation: The base condition is immediately satisfied by an empty stack. The copied stack is left empty and no operations are carried out.

Approach 1: Using Recursion (Core Approach)

The above approach uses the call stack to simulate an auxiliary structure through recursion. To maintain order, elements are added at the bottom of the cloned stack after being popped recursively until the stack is empty. Backtracking is the process of returning to the original stack. In order to guarantee proper ordering, the helper function reverses insertion.

Output:

Original Stack: [1, 2, 3]
Cloned Stack: [3, 2, 1]

Complexity Analysis

The time Complexity of the above code is O(n²) and the space Complexity of the above approach is O(n).

Approach 2: Using Single Stack Trick (Reversing Twice)

Recursive reversal is used in this method to access elements in FIFO order. Elements are iterated and added to the cloned stack after being reversed. Lastly, another reversal is used to restore the original stack. Recursion is still used internally, although explicit auxiliary structures are avoided.

Output:

Original Stack: [10, 20, 30]
Cloned Stack: [30, 20, 10]

Complexity Analysis

The time Complexity of the above code is O(n²) and the space Complexity of the above approach is O(n).

Approach 3: Using Pure Recursion with Return Stack

Instead of providing the cloned stack as an argument, this method creates it through recursive returns. To preserve order, elements are added at the bottom of each partial clone that is created by a recursive call. Data loss is prevented by restoring the original stack.

Output:

Original Stack: [5, 15, 25]
Cloned Stack: [25, 15, 5]

Complexity Analysis

The time Complexity of the above code is O(n²) and the space Complexity of the above approach is O(n).