I am getting error(Runtime)


#1
ListNode* Solution::getIntersectionNode(ListNode* A, ListNode* B) {
    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;
}

#2

while(stk1.top()==stk2.top()){
r=stk1.top();
stk1.pop();
stk2.pop();
}

in this, you are not covering a lot of cases,
for example, let
stk1 = 2->3->4
stk2 = 5->6->7->3->4
here common node is 3
but when you start popping from stack together stk1 and stk2 there is no chance their top will ever be equal ;
if you want to use this method then you will n^2 complexity ;