Rotate List || LeetCode
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* rotateRight(struct ListNode* head, int k) { struct ListNode *sentry,*p,*temp,*tail; int len=0,index; if(head==NULL)return NULL; sentry=(struct ListNode *)malloc(sizeof(struct ListNode)); sentry->next=NULL; p=head; tail=sentry; while(1){ temp=(struct ListNode*)malloc(sizeof(struct ListNode)); temp->val=p->val; temp->next=NULL; tail->next=temp; tail=temp; len++; if(p->next==NULL)break; p=p->next; } p->next=sentry->next; k=k%len,index=0; p=head; while(p){ if(index==(len-k)){ temp=p; } if(index==(2*len-k-1)){ p->next=NULL; break; } index++; p=p->next; } return temp; }