Skip to content
Codescope

Singly linked lists

Every node knows only the next one. Everything the structure can and cannot do follows from that.

core10 min8 steps

Before you start

A **linked list** is the opposite trade-off from an array. Instead of one block of memory, it is a chain of **nodes** scattered across the heap — each node holds a value plus a pointer to the next node.

That indirection is the whole story. You can insert or delete a node in the middle without moving anything else (just re-wire the pointers). But to reach the 500th element, you have to walk from the start, one pointer at a time.

This lesson watches nodes get allocated on the heap, linked together, spliced apart, and shows the classic bug — leaving a dangling pointer to a freed node.

Heap

nodes are not adjacent in memory
head
entry point

head is NULL — an empty list is just a pointer to nothing.

Step 1 of 8. A node is a value and a next pointer. Each node holds its data and the address of the node after it. There is no array, no block of contiguous memory — each node was allocated separately and could be anywhere on the heap. The `head` pointer is the only way in. Lose it and the entire list is unreachable.

01 / 08

Step 01 / 08

A node is a value and a next pointer

Each node holds its data and the address of the node after it. There is no array, no block of contiguous memory — each node was allocated separately and could be anywhere on the heap.

The head pointer is the only way in. Lose it and the entire list is unreachable.

linked-list.cC
struct Node {    int          value;    struct Node *next;};struct Node *head = NULL;/* insert at the front — O(1) */struct Node *push_front(struct Node *head, int value) {    struct Node *node = malloc(sizeof *node);    node->value = value;    node->next  = head;    return node;}/* find a value — O(n) */struct Node *find(struct Node *head, int target) {    for (struct Node *p = head; p != NULL; p = p->next)        if (p->value == target)            return p;    return NULL;}