Construct a singly linked list by allocating one node per value and chaining next pointers. Establishes the node + head + tail model used by every later linked-list lesson.

Algorithm

Canonical input [10, 20, 30, 40] builds the chain head -> 10 -> 20 -> 30 -> 40 -> null with one node appended per step.

Basic Implementation

basic.dart
class ListNode {
  int value;
  ListNode? next;
  ListNode(this.value) : next = null;
}

void main() {
  final values = <int>[10, 20, 30, 40];
  ListNode? head;
  ListNode? tail;
  for (final v in values) {
    final node = ListNode(v);
    if (head == null) {
      head = node;
    } else {
      tail!.next = node;
    }
    tail = node;
  }
}

Complexity

  • Time: O(n) with a tail pointer
  • Space: O(n)

Implementation notes

  • Dart: a small class ListNode { int value; ListNode? next; ... } is the idiomatic Node. The ? makes next nullable so the tail can carry null.
  • The replay never shows object identity; nodes are labelled node(<value>) and the chain view is rendered as 1 -> 2 -> ... -> null.
node chain Each node carries a value and a `next` reference to the following node or `null`.