# New LinkedList API footgun

**URL:** <https://ziggit.dev/t/new-linkedlist-api-footgun/10853>\
**Category:** Explain\
**Tags:** standard-library\
**Created:** [July 7, 2025, 7:31pm UTC](https://ziggit.dev/t/new-linkedlist-api-footgun/10853 "2025-07-07T19:31:56Z")\
**Posts on this page:** 1\
**Page:** 2

<div class="post-metadata">

**Author:** ![mnemnion](https://ziggit.dev/user_avatar/ziggit.dev/mnemnion/32/2478_2.png) [@mnemnion](https://ziggit.dev/u/mnemnion)\
**Post date:** [July 13, 2025, 4:59pm UTC](https://ziggit.dev/t/new-linkedlist-api-footgun/10853/21 "2025-07-13T16:59:32Z")

</div>

> [@prokoprandacek](#):
>
> The [current implementation](https://github.com/ziglang/zig/blob/master/lib/std/SinglyLinkedList.zig#L88) crashes when the list is empty

Let’s compare then, here’s stdlib:

```zig
pub fn remove(list: *SinglyLinkedList, node: *Node) void {
    if (list.first == node) {
        list.first = node.next;
    } else {
        var current_elm = list.first.?;
        while (current_elm.next != node) {
            current_elm = current_elm.next.?;
        }
        current_elm.next = node.next;
    }
}

```

Zelda:

```zig
pub fn remove(list: *SinglyLinkedList, node: *Node) void {
    if (list.first == node) {
        list.first = @field(node, next);
        @field(node, next) = null;
    } else {
        var current_elm = list.first.?;
        while (@field(current_elm, next)) |next_elm| {
            if (next_elm == node) {
                @field(current_elm, next) = @field(node, next);
                @field(node, next) = null;
            } else {
                current_elm = next_elm;
            }
        }
    }
}

```

The obvious difference is that Zelda’s has a null check for each iteration (the capture does that). This should be assumed to have some non-zero efficiency cost, although I do wonder if it would be visible: pointer chasing is going to cost a few cycles, and branch prediction will be biased toward the fetch.

But it’s a justifiable choice because, again, there’s no reason to be removing things from a list which aren’t actually there to begin with. I just deeply hate debugging linked list problems so I coded Zelda somewhat defensively, setting the `next` field to `null` is another example of that.

Zelda’s version also keeps walking the list, which is silly, I’ll add a break statement there. So I’m glad I checked.

[Previous page](https://ziggit.dev/t/new-linkedlist-api-footgun/10853.md?page=1)
