Why my code is giving runtime error?


#1
ListNode* Solution::getIntersectionNode(ListNode* A, ListNode* B) {
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
    stack<ListNode*> stk1;
    stack<ListNode*> stk2;
    
    ListNode* p=A;
    ListNode* q=B;
    ListNode* r=NULL;
    while(p!=NULL){
        stk1.push(p);
        p=p->next;
    }
    while(q!=NULL){
        stk2.push(q);
        q=q->next;
    }
    if(stk1.top()==NULL || stk2.top()==NULL){
        return 0;
    }
    while(stk1.top()==stk2.top()){
        r=stk1.top();
        stk1.pop();
        stk2.pop();
    }
    return r;
}