-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRotateList.java
53 lines (48 loc) · 1.28 KB
/
RotateList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Created by cpacm on 2017/6/29.
*/
public class RotateList {
public static void main(String[] args) {
ListNode head = new ListNode(10);
head.next = new ListNode(11);
head.next.next = new ListNode(12);
head.next.next.next = new ListNode(13);
head.next.next.next.next = new ListNode(14);
head.next.next.next.next.next = new ListNode(15);
head.next.next.next.next.next.next = new ListNode(16);
System.out.println(rotateRight(head, 4));
}
public static ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
int count = 1;
ListNode temp = head;
ListNode last = head;
while (last.next != null) {
last = last.next;
count++;
}
k = k % count;
int index = count - k;
if (index <= 0) {
return head;
}
index--;
while (index > 0) {
temp = temp.next;
index--;
}
last.next = head;
head = temp.next;
temp.next = null;
return head;
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}