forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateLinkedList.java
77 lines (67 loc) · 1.88 KB
/
RotateLinkedList.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
71
72
73
74
75
76
77
package com.rampatra.linkedlists;
import com.rampatra.base.SingleLinkedList;
import com.rampatra.base.SingleLinkedNode;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 7/3/15
* @time: 3:07 PM
*/
public class RotateLinkedList {
/**
* Rotates the {@param list} anti-clockwise by {@param k} nodes.
*
* @param list
* @param k
* @param <E>
*/
public static <E extends Comparable<E>> void rotateCounterClockwise(SingleLinkedList<E> list, int k) {
int clockwiseK = list.size - k;
rotateClockwise(list, clockwiseK);
}
/**
* Rotates the {@param list} clockwise by {@param k} nodes.
* <p/>
* Example,
* <p/>
* Input: [0,11,22,33,44,55] and k = 2
* Output: [22,33,44,55,0,11]
*
* @param list
* @param k
* @param <E>
*/
public static <E extends Comparable<E>> void rotateClockwise(SingleLinkedList<E> list, int k) {
int i = 0;
SingleLinkedNode<E> curr = list.head, end = curr;
// get a pointer to the last node
while (end.next != null) {
end = end.next;
}
// start moving first k nodes from start to end
while (i < k && k < list.size) {
end.next = curr;
end = end.next;
curr = curr.next;
i++;
}
// change head to k+1 node
list.head = curr;
end.next = null;
}
public static void main(String[] args) {
SingleLinkedList<Integer> linkedList = new SingleLinkedList<>();
linkedList.add(00);
linkedList.add(11);
linkedList.add(22);
linkedList.add(33);
linkedList.add(44);
linkedList.add(55);
linkedList.printList();
rotateClockwise(linkedList, 2);
linkedList.printList();
rotateCounterClockwise(linkedList, 2);
linkedList.printList();
}
}