1->2->3->4->5 ==> 4->5->1->2->3
1,遍历链表找到最后一个节点,同时计算出长度N
2,最后一个节点指向头结点,形成循环链表
3,然后从头节点向后移动(N-K) 节点,并把当前节点的 next设置为NULL
4,注意循环K需要模上链表长度N
struct ListNode* rotateRight(struct ListNode* head, int k) {
if(head == NULL || head->next == NULL)
return head;
struct ListNode *fast, *tmp;
fast = head;
int i = 1;
//cycle first
while(fast->next){
fast = fast->next;
i++;
}
fast->next = head;
fast = head;
k = i-k%i;
while(--k){ //not k-- 注意
fast = fast->next;
}
tmp = fast->next;
fast->next = NULL;
return tmp;
}