LeetCode OJ Problem: Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given1->2->3->4->5->NULLAnd k =2,
Return4->5->1->2->3->NULL.
Class Solution {public: ListNode * rotateRight (ListNode * head, int k) {if ((! Head) | (! Head-> next) | (k = 0) return head; ListNode * p, * q; int m, n; m = k; n = 0; // if k is greater than the length of the linked list, n is used to record the length of the linked list p = q = head; while (m & p) {p = p-> next; m --; n ++;}/* if the k value is greater than the length of the linked list, then k is used to cycle the remainder of the linked list length */if (! P) {n = k % n; if (n = 0) return head; p = head; while (n) {p = p-> next; n --;}} /* p goes k steps first, and then p and q go at the same time until p reaches the end of the linked list, then q also takes k steps */while (p-> next) {q = q-> next; p = p-> next;} p-> next = head; head = q-> next; q-> next = NULL; return head ;}};