k places, where k
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
Subscribe to see which companies asked this question
比如说 [1],1这样的数据。
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (head == NULL||k<=0) return head;
ListNode* p = head;
int len = 0;
while (p){
p = p->next;
len++;
}
k = k%len;///
if (k == 0) return head;//
ListNode* faster = head;
ListNode* slower = head;
while (k--){
faster = faster->next;
}
while (faster->next){
faster = faster->next;
slower = slower->next;
}
ListNode* newHead = slower->next;
slower->next = NULL;
faster->next = head;
return newHead;
}
};