Run properly with Run with Costume input but in main submission it give wrong answer can anyone help


#1

struct node{
char val ;
struct node* next;
};
struct node* head=NULL;

void push(char n){
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->val= n;
temp->next= head;
head= temp;
}

int k=0;

void pop(){
if(head==NULL){
k=1;
}
else{
head= head->next;

}

}

int stacktop(){
if(head==NULL){
return ‘0’;
}
else{
return head->val;
}}

int Solution::solve(string A) {
for(int i =0; i<A.length();i++){
if(A[i]==’(’){
push(’(’);
}
else{
pop();
}
}
if(stacktop()==‘0’ && k==0){
return 1;
}
else{
return 0;
}

}


#2

Try clearing global variable after using it or before using it.