rotate method
In-place rotate the queue steps
steps to the right. If steps
is
negative, the list is rotated to the left.
Rotating one step to the right (steps = 1) is equivalent to
queue.addFirst(list.removeLast())
. Rotating one step to the left
(step = -1) is equivalent to list.addLast(list.removeFirst())
.
Implementation
void rotate(int steps) {
final length = this.length;
if (length > 1) {
var count = steps % length;
if (count != 0) {
var other = length - count;
if (count <= other) {
while (count-- > 0) {
addFirst(removeLast());
}
} else {
while (other-- > 0) {
addLast(removeFirst());
}
}
}
}
}