0Pricing
Zig Academy · Lesson

A Singly Linked List

Nodes, ownership, and traversal.

A Singly Linked List is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What a Linked List Is

A singly linked list chains values together: each node holds a value and a pointer to the next node, or null at the end. 🔗

Define the Node

A node is a struct with a value and an optional pointer to the next node. Optional means the last node points at null.

const Node = struct {
    value: i32,
    next: ?*Node,
};

Why the Pointer Is Optional

Using ?*Node lets the chain end safely. There is no need for a special sentinel value to mark the tail.

next: ?*Node

The List Tracks the Head

The list itself just remembers the first node, called the head, plus the allocator that owns every node.

const List = struct {
    head: ?*Node,
    alloc: std.mem.Allocator,
};

Allocate One Node

To add a value, ask the allocator to create a single Node on the heap. This returns a pointer to fresh storage.

const node = try self.alloc.create(Node);

Push to the Front

The cheapest insert is at the head: point the new node at the old head, then make it the new head. This is constant time.

node.* = .{ .value = v, .next = self.head };
self.head = node;

Walk the Chain

To traverse, start at head and follow each next pointer until you reach null. A while loop captures the node nicely.

var cur = self.head;
while (cur) |n| {
    std.debug.print("{d}\n", .{n.value});
    cur = n.next;
}

The Capture Unwraps the Optional

Writing while (cur) |n| runs the body only while cur is non-null, and n is the unwrapped pointer. The loop ends at null.

while (cur) |n| { ... }

You Own Every Node

Each node came from the allocator, so each must be returned. Forgetting even one is a leak the testing allocator will report.

Free the Whole List

Walk the chain in deinit, but grab next before freeing, because the node is gone the moment you destroy it.

var cur = self.head;
while (cur) |n| {
    const nxt = n.next;
    self.alloc.destroy(n);
    cur = nxt;
}

Make It Generic

Wrap Node and List in a type-returning function so the list works for any element type, not just i32.

fn LinkedList(comptime T: type) type {
    return struct {
        const Node = struct { value: T, next: ?*Node };
    };
}

Quick Check

You are freeing a linked list node by node. What must you do before destroying each node?

Recap

Nodes hold a value and an optional next pointer. The list tracks the head and an allocator, pushes to the front, and frees node by node. 🎯

Frequently asked questions

Is the “A Singly Linked List” lesson free?

Yes — the full text of “A Singly Linked List” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “A Singly Linked List”?

Nodes, ownership, and traversal. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “A Singly Linked List” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. A Generic Stack from Scratch
  2. A Singly Linked List
  3. Using HashMap and AutoHashMap
  4. Profiling and Safety Trade-offs
← Back to Zig Academy