class Solution {
ListNode tail = null;
public int getLen(ListNode head) {
int len = 0;
while (head != null) {
len++;
tail = head;
head = head.next;
}
return len;
}
public ListNode rotateRight(ListNode head, int k) {
int len = getLen(head);
if (len < 2) {
return head;
}
k %= len;
if (k == 0) {
return head;
}
int lk = len - k;
ListNode root = new ListNode(), p = root, q = null, r= null;
root.next = head;
int i = 0;
while (i < lk) {
p = p.next;
i++;
}
q = p.next;
p.next = null;
r = root.next;
root.next = q;
tail.next = r;
return root.next;
}
}
10-16
228
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-13
336
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-12
375
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-08
436
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交