-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution4.java
70 lines (68 loc) · 1.39 KB
/
Solution4.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
68
69
70
/*
* Rotate List
* Given 1->2->3->4->5->NULL and k = 2,
* return 4->5->1->2->3->NULL.
*/
public class Solution4 {
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode rotateRight(ListNode head, int n) {
if (head == null || head.next == null || n == 0)
return head;
int length = 0;
ListNode node = head;
while (node != null) {
length++;
node = node.next;
}
int[] numbers = new int[length];
node = head;
int index = 0;
while (node != null) {
numbers[index++] = node.val;
node = node.next;
}
swap(numbers, 0, length-(n%length)-1);
swap(numbers, length-(n%length), length-1);
swap(numbers, 0, length-1);
node = head;
for (int i : numbers) {
node.val = i;
node = node.next;
}
return head;
}
public void swap(int[] numbers, int begin, int end) {
int i = begin;
int j = end;
while (i < j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++;
j--;
}
}
public static void main(String... args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l1 = new Solution4().rotateRight(l1, 1);
while (l1 != null) {
System.out.print(l1.val + " ");
l1 = l1.next;
}
}
}