rotateRight method

void rotateRight(
  1. int k
)

Rotates the slice in-place such that the first this.len() - k elements of the slice move to the end while the last k elements move to the front. After calling rotate_right, the element previously at index this.len() - k will become the first element in the slice.

Implementation

void rotateRight(int k) {
  int length = len();
  if (k > length) {
    throw ArgumentError("mid is greater than the length of the list.");
  }
  if (k == length || k == 0) {
    return;
  }

  k = length - k; // Adjust mid for right rotation
  _reverse(this, 0, k - 1);
  _reverse(this, k, length - 1);
  _reverse(this, 0, length - 1);
}