Linked List
A singly linked list is a chain of nodes. Each node holds a value and a pointer to the next node. The list is the pointer to the first node, or NULL when empty.
Detailed Explanation
Arrays give you contiguous storage and O(1) index. A linked list gives you O(1) insert and delete at a position you already hold, and O(n) to get there. In C the node is a struct:
struct Node {
int value;
struct Node *next;
};
The self-pointer is allowed because a pointer to an incomplete type is fine. The list head is a struct Node *. An empty list is NULL. Some programs keep a dummy head node so insert-at-front and insert-after have the same code; that dummy is never a real element.
Every node is usually malloc'd. Every node must be free'd. Walking the list to free it needs a copy of next before you free the current node, or you lose the rest of the chain. Inserting at the front is three assignments: allocate, set next to the old head, set the head to the new node. Deleting the first node is the reverse.
The cost is not only the pointer. Nodes live on the heap and are rarely adjacent, so a walk misses cache. An array of the same ints is often several times faster to scan. Reach for a list when you need stable pointers to elements under insert and delete, when you splice lists, or when the language of the problem is already a chain (an intrusive list in a kernel, a free list of reused nodes).
Intrusive lists put the next pointer inside the object the list owns, instead of wrapping the object in a node. They avoid a second allocation and are how many C codebases actually ship lists. The idea is the same: a pointer, a NULL terminator, a walk.
A cycle — p->next eventually reaching p — turns every walk into an infinite loop. Building a list from untrusted input without a visited set, or mishandling next during a splice, is how cycles appear. Floyd's two-pointer check can detect them; not creating them is cheaper.
Code Examples
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
struct Node *next;
};
static struct Node *push_front(struct Node *head, int value)
{
struct Node *n = malloc(sizeof *n);
if (n == NULL) {
return NULL; /* caller keeps the old head */
}
n->value = value;
n->next = head;
return n;
}
static void list_free(struct Node *head)
{
while (head != NULL) {
struct Node *next = head->next; /* save before free */
free(head);
head = next;
}
}
int main(void)
{
struct Node *head = NULL;
head = push_front(head, 3);
head = push_front(head, 2);
head = push_front(head, 1);
for (struct Node *p = head; p != NULL; p = p->next) {
printf("%d ", p->value);
}
printf("\n"); /* prints: 1 2 3 */
list_free(head);
return 0;
}Common Gotchas
`free(p)` and then `p->next` is use-after-free
copy `next` first.
Losing the last pointer to a node leaks it
`head = head->next` without freeing `head` drops that node on the floor.
`push_front` can fail
returning `NULL` must not be treated as "empty list" if the old list still exists. Separate the error from the head, or use an out-parameter.
Indexing a list is `O(n)`
if you write `for (i) { walk i steps }` you have accidentally made an `O(n^2)` algorithm.