reverse function

  1. @Possible({RangeError})
void reverse(
  1. List<Object?> list, [
  2. int start = 0,
  3. int? end
])

Reverses a list, or part of between start, inclusive and end, exclusive, in-place.

Contract

0 <= start < end <= list's length. Throws a RangeError otherwise.

Example

final list = [0, 1, 2, 3, 4];
reverse(list, 1, 5);

print(list); // [0, 4, 3, 2, 1]

Implementation

@Possible({RangeError})
void reverse(List<Object?> list, [int start = 0, int? end]) {
  end = RangeError.checkValidRange(start, end, list.length);
  for (var i = start, j = end - 1; i < j; i++, j--) {
    final (a, b) = (list[i], list[j]);
    list[i] = b;
    list[j] = a;
  }
}