Solution in c: easy to understand

amazon
Tags: #<Tag:0x00007f2423eb0b38>

#1

listnode* rotateRight(listnode* A, int B) {
if(A==NULL || A->next==NULL)
return A;
int c=0,i;
int pos;
listnode *p=A,*q;

while(p)
{
   c++;
   p=p->next;
}
B=B%c;
pos=c-B;
if(pos==c)
    return A;
p=A;
q=p->next;
for(i=0;i<pos-1;i++)
{
    p=p->next;
    q=p->next;
}
p->next=NULL;
listnode *r=q;
while(r->next!=NULL)
{
    r=r->next;
}
r->next=A;
return q;