给单链表一个k数量节点的旋转。
开始WA,后来发现需要对k取mod。
class Solution { public: ListNode *rotateRight(ListNode *head, int k) { int height = heightList(head); if(height == 0)return head; k %= height; if(height < k || head == NULL || head->next == NULL || k == 0)return head; k = height - k; ListNode * before = head; ListNode * now = head->next; k--; while(k > 0) { k--; before = now; now = now->next; } ListNode * tmp = head; while(tmp->next != NULL) { tmp = tmp->next; } tmp->next = head; before->next = NULL; return now; } private: int heightList(ListNode * head) { int height = 0; while(head != NULL) { head = head->next; height++; } return height; } };