61. 旋转链表
题解
形成环、截断


class Solution {public ListNode rotateRight(ListNode head, int k) {if (k == 0 || head == null || head.next == null) {return head;}int count = 1;ListNode tail = head;while (tail.next != null) { // 遍历整个链表,求出链表长度 counttail = tail.next;count++;}k = k % count; // 由于 k 可能很大,为了避免不必要的计算,可以将 k 取余ListNode p = head;for (int i = 0; i < count - k - 1; i++) { // 找到第 count - k 个节点p = p.next;}tail.next = head;head = p.next;p.next = null;return head;}}

