Clean C solution

• ``````/**
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
struct ListNode* reverse(struct ListNode* head) {
struct ListNode* t = head, *prev = 0;

t->next = prev;
prev = t;
}
return t;
}
int c = 0;
c++;
}
return c;
}
for (int i = 0; i < n && head; i++){
}
}

// rotate could be implemented as 3 reverse, e.g: 1234abc -> 1234(cba) ->  (abc4321) -> abc(1234)
struct ListNode* rotateRight(struct ListNode* head, int k) {
struct ListNode* t;
if (!head || k % n == 0) {
}

t = advance(head, n - k % n - 1); // find t where t->next is a list with exactly n - k nodes
t->next = reverse(t->next); // reverse the tailing n-k nodes, remember to connect it back to the original list

t = advance(head, k % n - 1); // find t where t->next is a list with exactly k nodes
t->next = reverse(t->next); // reverse the tailing k nodes, and connect it back

}
``````

Please note that the above implementation runs 4 pass but seems a little bit clear than the following implementation, which takes 3 + k/n pass.

``````// rotate could be implemented as 3 reverse, e.g: 1234abc -> (cba4321) -> (abc)(1234)
struct ListNode* rotateRight(struct ListNode* head, int k) {
struct ListNode* t, *t2;

if (!head || k % n == 0) {
}

k %= n;

// step 1: reverse whole list

// step 2: find the split position