Solution using two pointer method


#1

int Solution::solve(ListNode* A, int B) {
ListNode* fast=A;
ListNode* slow=A;
for(int i=0;i<B;i++){
if(fast && fast->next)
{
fast=fast->next->next;
}else{
return -1;
}
}
while(fast && fast->next){
slow=slow->next;
fast=fast->next->next;
}
return slow->val;
}