Single pass O(n) time O(1) space C++


#1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
int Solution::solve(ListNode* A, int B) {
    int move = 0;
    ListNode* target = nullptr;
    ListNode* fast = A;
    while(fast and fast->next)
    {
        fast = fast->next->next;
        move++;
        if(move == B) target = A;
        else if(move>B) target = target->next;
    }
    return target? target->val : -1;
}