ForwardList<T>.from constructor

ForwardList<T>.from(
  1. Iterable<T> elements
)

Creates a forward list from an iterable. Elements are added in the order they appear, so the first element of the iterable will be at the front of the forward list.

Implementation

ForwardList.from(Iterable<T> elements) {
  if (elements.isEmpty) return;

  var iter = elements.iterator;
  iter.moveNext();

  _head = _ForwardListNode<T>(iter.current);
  _length = 1;

  var current = _head;
  while (iter.moveNext()) {
    current!.next = _ForwardListNode<T>(iter.current);
    current = current.next;
    _length++;
  }
}