Delete Mid element of stack
Here we are having length of stack.
So we will go till n/2 and pop elements if it is < n/2
Call function recursively
if we are at n/2 node , delete it
Code:
public Stack<Integer> deleteMid(Stack<Integer> s,int n,int current){
if(!s.isEmpty() && current<n/2)
{
int x=s.pop();
deleteMid(s,n,current+1);
s.push(x);
}
if(!s.isEmpty() && current ==n/2)
s.pop();
return s;
}
So we will go till n/2 and pop elements if it is < n/2
Call function recursively
if we are at n/2 node , delete it
Code:
public Stack<Integer> deleteMid(Stack<Integer> s,int n,int current){
if(!s.isEmpty() && current<n/2)
{
int x=s.pop();
deleteMid(s,n,current+1);
s.push(x);
}
if(!s.isEmpty() && current ==n/2)
s.pop();
return s;
}
0 Comments:
Post a Comment