reverse method

void reverse()

Reverses the list in place (O(N) time complexity). This is highly efficient as it just redirects the underlying node pointers.

Implementation

void reverse() {
  _ForwardListNode<T>? prev;
  _ForwardListNode<T>? current = _head;
  _ForwardListNode<T>? next;

  while (current != null) {
    next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }

  _head = prev;
}