Learn C
#include "systems/linked_list.h"

Capstone: Linked List in C

#concept

A singly linked list chains heap nodes (value plus next pointer) through head-pointer surgery: prepend links a fresh node atop, append walks to the tail, delete unlinks and frees, destroy walks the whole chain. Every operation is pointer manipulation learned across the pointers, structs, and memory modules — the course's comprehensive final exam.

Lists trade cache-hostile O(n) indexing for O(1) head insertion and never-invalidated node addresses. Implement prepend/append/find/delete/destroy plus a print helper, test under AddressSanitizer until clean, and the language's hardest mechanics become muscle memory.

#how it works
1

Nodes chain via next

struct Node { int value; struct Node *next; }; — each node points onward; NULL terminates the chain.

2

Prepend in O(1)

New node's next takes the head, head takes the node — constant-time front insertion, the list's superpower.

3

Unlink then free

Bypass the victim (prev->next = victim->next, or move head), then free it — order matters, reversed order dangles.

4

Destroy by walking

Save next, free current, advance — the only safe traversal order, verified leak-free under sanitizers.

#example
#include <stdio.h>
#include <stdlib.h>
 
typedef struct Node {
int value;
struct Node *next;
} Node;
 
void prepend(Node **head, int v) {
Node *n = malloc(sizeof *n);
if (!n) return;
n->value = v;
n->next = *head;
*head = n;
}
 
int main(void) {
Node *head = NULL;
prepend(&head, 2);
prepend(&head, 1);
printf("%d %d\n", head->value, head->next->value);
return 0;
}
Output
1 2
1Double pointer lets prepend rewrite the caller's head — single pointers couldn't update it.
2Prepending 2 then 1 builds 1 -> 2 -> NULL — each new node takes the front.
31 2 prints following next pointers — (a real program would free the chain before exit.)
#complexity
time O(1) prepend, O(n) search/append
space O(n)

Per-node malloc overhead; node addresses never invalidate.

#use cases
O(1) front insertion where arrays would shift
Stable node addresses across arbitrary growth
Stacks and queues with pointer-based implementations
Sanitizer-verified manual memory management practice

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.