-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRorateRight.java
57 lines (50 loc) · 1.14 KB
/
RorateRight.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
54
55
56
57
package ProjectFiles;
public class RorateRight {
public static void main(String[] args) {
ListNode h = new ListNode(1);
h.next = new ListNode(2);
h.next.next = new ListNode(3);
// ListNode t = h.next.next;
// t.next = new ListNode(2);
// t.next.next = new ListNode(1);
// t.next.next.next = null;
ListNode root = h;
root = rotateRight(root, 6);
while(root != null){
System.out.println(root.val);
root = root.next;
}
}
private static ListNode rotateRight(ListNode head, int k) {
if(k == 0 || head == null || head.next == null){
return head;
}
ListNode headNode = head;
ListNode tailNode ;
int length = 1;
int i = 1;
boolean isLengthShoterThanK = false;
while( i <= k){
if(headNode.next == null){
isLengthShoterThanK = true;
break;
}
headNode = headNode.next;
length++;
i++;
}
if(isLengthShoterThanK){
return rotateRight(head , k % length );
}
tailNode = head;
while(headNode.next != null){
tailNode = tailNode.next;
headNode = headNode.next;
}
ListNode start = tailNode.next;
tailNode.next = null;
headNode.next = head;
head = start;
return head;
}
}