O(n) time & O(1) space simple, short and clear c++ solution


#1
ListNode* Solution::solve(ListNode* A, int b) {
    if(A == NULL) return NULL;
    ListNode *head,*curr,*next,*prev;
    curr = A; prev = NULL;
    for(int i = 0; i<b;i++){
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    head = prev;
    A->next = curr;
    if(curr==NULL) return head;
    
    for(int i = 0; i<b-1;i++)
    curr = curr->next;
    
    curr->next = solve(curr->next,b);
    return head;
}

#2

This is not O(1) since is a recursive solution.