链表旋转链表
难度中等
题目描述
解题思路


作者:demigodliu
链接:https://leetcode-cn.com/problems/rotate-list/solution/bi-he-wei-huan-xuan-zhuan-lian-biao-by-d-3wj1/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Code
public ListNode rotateRight(ListNode head, int k) {if (k == 0 || head == null || head.next == null) {return head;}int len = 1;ListNode cur = head;while (cur.next != null) {cur = cur.next;len++;}int add = len - k % len;cur.next = head;while (add-- > 0) {cur = cur.next;}ListNode ans = cur.next;cur.next = null;return ans;}
