1
class Solution {
2
public ListNode rotateRight(ListNode head, int k) {
3
if (k <= 0 || head == null || head.next == null) {
4
return head;
5
}
6

7
int length = 1;
8
ListNode first = head;
9
ListNode curr = head;
10
ListNode node = head;
11
while (node.next != null) {
12
length++;
13
node = node.next;
14
}
15

16
if (k == length) {
17
return head;
18
}
19

20
int n = length - (k % length);
21
for (int i = 0; i < n - 1; i++) {
22
curr = curr.next;
23
}
24

25
node.next = head; // 5-->1
26
head = curr.next;
27
curr.next = null;
28

29
return head;
30
}
31
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0