public ListNode rotateRight(ListNode head, int k){ if (head == null || head.next == null) return head; ListNode pre = head, post = head; int count = 1; while (pre.next != null){ //遍历1次获得链表长度 pre = pre.next; count++; } k = k % count; //计算位置 pre = head; while (k-- != 0) pre = pre.next; //就不用头尾循环了 while (pre.next != null){ pre = pre.next; post = post.next; } pre.next = head; head = post.next; post.next = null; return head; }