-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotateRightSolution.java
70 lines (64 loc) · 1.59 KB
/
RotateRightSolution.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
58
59
60
61
62
63
64
65
66
67
public class RotateRightSolution {
/**
* @param head: the List
* @param k: rotate to the right k places
* @return: the list after rotation
*/
public ListNode rotateRight(ListNode head, int k) {
// write your code here
if(head == null || head.next == null){
return head;
}
ListNode preHead = new ListNode(0);
preHead.next = head;
ListNode p1 = head;
ListNode p2 = head;
ListNode helper = head;
int count = 0;
while(helper != null){
helper = helper.next;
count++;
}
if(k >= count ){
k = count - k/count;
}
if(k == 0){
return head;
}
//k = count - k/count;
int i = 0;
while(i < k){
i++;
p2 = p2.next;
if( p2.next == null){
p2 = head;
}
}
if(p2 == null || p2.next == null){
return head;
}
while(p2.next != null){
p1 = p1.next;
p2 = p2.next;
}
p2.next = head;
preHead.next = p1.next;
p1.next = null;
return preHead.next;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public static void main(String args[]){
//int[] A = {-3,1,1,-3,5};
ArrayList<Integer> A = new ArrayList<Integer>();
A =
//Integer a = new Integer(1);
System.out.println(rotateRight(A));
}
}