rotate method
In-place rotate the list 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
list.insert(0, list.removeLast())
. Rotating one step to the left
(step = -1) is equivalent to list.add(list.removeAt(0))
.
Implementation
void rotate(int steps) {
final length = this.length;
if (length > 1) {
final count = steps % length;
if (count != 0) {
final other = length - count;
if (count <= other) {
final copy = sublist(other);
setRange(count, length, this);
setRange(0, copy.length, copy);
} else {
final copy = sublist(0, other);
setRange(0, count, this, other);
setRange(count, length, copy);
}
}
}
}