Simple Interview Answer!


#1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
ListNode* Solution::rotateRight(ListNode* A, int B) {
    if(A->next==nullptr || B==0)return A;
    ListNode* It=A;
    int n=0;
    while(It)It=It->next,n++;
    B%=n;
    if(B==0)return A;
    ListNode*H=A,*M=A,*prev;
    while(B--)M=M->next;
    while(M){
        prev=H;
        H=H->next;
        M=M->next;
    }
    prev->next=nullptr;
    It=H;
    while(It->next)It=It->next;
    It->next=A;
    return H;
}