1 public class Solution { 2 public ListNode rotateRight(ListNode head, int n) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(head == null || n == 0){ 6 return head; 7 } 8 int len = 1; 9 ListNode p = head; 10 while(p.next != null){ 11 len ++; 12 p = p.next; 13 } 14 p.next = head; 15 16 int count = (len - n) % len; 17 while(count < 0){ 18 count += len; 19 count %= len; 20 } 21 int step = 0; 22 while(step < count){ 23 p = p.next; 24 step ++; 25 } 26 head = p.next; 27 p.next = null; 28 return head; 29 } 30 }