ListNode* rotateRight(ListNode* head, int k) {
if (head == NULL || head->next == NULL) return head;
ListNode *pNode = head;
int n = 1;
while (pNode->next) {
pNode = pNode->next;
++n;
}
pNode->next = head;
int step = n - k%n;
while (step) {
pNode = head;
head = head->next;
step--;
}
pNode->next = NULL;
return head;
}
网友评论