Singly Linked List in C and C++
Chain nodes together with pointers so you can insert without shifting elements, then free every node safely.
- Node structure: a value plus a next pointer
- Building and walking the list
- Freeing nodes in C and unique_ptr in C++
How nodes link together
The problem
Inserting at the front of an array means shifting every element one place. When items are added and removed often, a structure that can link new items in without moving the others is useful.
Think of it like this
A linked list is like a treasure hunt. Each clue (node) holds a prize (value) and tells you where the next clue is hidden. You only need to know where the first clue is; the last clue says "the end".
Node *head🗺️ Where the first clue isnode->value🎁 The prize at this cluenode->next➡️ Directions to the next clueNULL🏁 The last clue: no moreA singly linked list is a chain of nodes, each holding a value and a pointer to the next node, reached by starting at a head pointer.
Important points
struct Nodeholdsvalueandnext, a pointer to the following node.- The loop inserts 3, 2, 1 at the front by setting the new node's
nextto the oldhead. - The
while (head)loop prints each value, savesnext, then callsfreeon the node. - In C++,
std::unique_ptr<Node>owns the next node, so the whole chain is freed automatically.
When to use it
- Frequent insertions and removals at the front or in the middle.
- Building other structures such as stacks, queues and hash table chains.
- Learning pointers and dynamic memory in practice.
When not to use it
- Accessing items by position: arrays and vectors find element i instantly.
- Most C++ code:
std::vectoris usually faster because its elements sit together in memory.
See how it works
Add nodes at the front or the back and click a node to delete it. Press Play to follow the next pointers from head to NULL.
C and C++ code examples
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node *next;
} Node;
int main(void) {
Node *head = NULL;
for (int value = 3; value >= 1; --value) {
Node *node = malloc(sizeof *node);
if (!node) return 1;
*node = (Node){value, head};
head = node;
}
while (head) {
Node *next = head->next;
printf("%d ", head->value);
free(head);
head = next;
}
puts("");
return 0;
}
1 2 3
#include <iostream>
#include <memory>
struct Node {
int value;
std::unique_ptr<Node> next;
};
int main() {
std::unique_ptr<Node> head;
for (int value = 3; value >= 1; --value) {
head = std::make_unique<Node>(Node{value, std::move(head)});
}
for (Node *node = head.get(); node; node = node->next.get()) {
std::cout << node->value << ' ';
}
std::cout << '\n';
}
1 2 3
C and C++ comparison
Each new node is placed in front of the current head, so inserting 3, then 2, then 1 gives the order 1 2 3. The while loop prints a node, remembers its next pointer, frees it and moves on. In C++, unique_ptr owns each next node, so destroying head releases the whole chain.
Structures and operations are usually separate and allocation is explicit.
Classes and containers can preserve invariants and manage resources automatically.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Add empty and capacity-boundary tests.
- Implement cleanup and verify no leaks remain.
- Compare operation complexity with a standard container.
Common mistakes
Freeing a node before saving next
After free, head->next must not be read.
free(head);
head = head->next;
Node *next = head->next;
free(head);
head = next;
Losing the rest of the list
Overwriting head without linking the old head leaks every node.
head = node; /* node->next not set */
node->next = head;
head = node;
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges