Simple solution with some debug positions


#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==NULL)
    {
    return A;
    }
    int size=1;
    ListNode* cur=A;
    while(cur->next!=NULL)
    {
    cur=cur->next;
    size++;
    }
    // cout<<size<<"\n";
    B=B%size;
    // cout<<B<<"\n";
    if(B==0)
    {
    return A;
    }
    cur->next=A;
    cur=A;
    for(int i=1;i<size-B;i++)
    {
    cur=cur->next;
    // cout<val<<"\n";
    }
    ListNode* temp=cur->next;
    A=temp;
    cur->next=NULL;
    return A;
    }