Code for the problem

include

using namespace std;

class node{

public:

int data;

node*next;

//constructor

node(int d){

 data = d;

 next = NULL;

   }

};

int length(node*head){

int len=0;

while(head!=NULL){

	head = head->next;

	len++;

}

return len;

}

void insertAtTail(node*&head,int data){

if(head==NULL){

head = new node(data);

	return;

}

node*tail = head;

while(tail->next!=NULL){

	tail = tail->next;

}

tail->next = new node(data);

return;

}

void intersection(nodea,nodeb){

int l1 = length(a);

int l2 = length(b);

int d = abs(l1-l2);

if(l1>l2){

    while(d--){

        a = a->next;

    }

    while(a->data!=b->data and a->next!=NULL){

        a = a->next;

        b = b->next;

        if(a->data==b->data){

            cout<<a->data;

            return;

        }

    }

}

else{

    while(d--){

        b = b->next;

    }

    while(b->data!=a->data and b->next!=NULL){

        a = a->next;

        b = b->next;

        if(a->data==b->data){

            cout<<b->data;

            return;

        }

    }

}

cout<<"-1";

}

void print(node*head){

while(head!=NULL){

	cout<<head->data<<" ";

	head = head->next;

}

cout<<endl;

}

int main(){

node*a = NULL;

node*b = NULL;

int n1,n2;

cin>>n1;

while(n1--){

    int num;

    cin>>num;

    insertAtTail(a,num);

}

cin>>n2;

while(n2--){

    int num;

    cin>>num;

    insertAtTail(b,num);

}

intersection(a,b);

return 0;

}

I have edited your code… Try to submit it now…