Very Easy to Understand Solution in C++


#1

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