// You are given access to functions push, pop and isempty
void reverse(stack s){
if(!isempty(s)){
int temp = pop(s);
reverse(s);
pushtobottom(s,temp);
}
}
pushtobottom(stack s, int data){
if(!isempty(s)){
temp = pop(s);
pushtobottom(s,data);
push(s,temp);
} else {
push(s,data);
}
}
this solution uses system stack which is a memory. So "without extra memory" is not correct statement.
ReplyDelete