Data Structures in C/C++

Singly Linked List in C and C++

Chain nodes together with pointers so you can insert without shifting elements, then free every node safely.

  • Intermediate
  • 4 min read
  • 2 runnable examples
What you'll learn
  • Node structure: a value plus a next pointer
  • Building and walking the list
  • Freeing nodes in C and unique_ptr in C++
head → 1 → 2 → 3
headpointer
1 →node
2 →node
3 →node
NULLend
Nodes can be anywhere in memory; only the next pointers keep their order.

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".

From a treasure hunt to code
Node *head🗺️ Where the first clue is
node->value🎁 The prize at this clue
node->next➡️ Directions to the next clue
NULL🏁 The last clue: no more
Each node points to the next; the list is walked from the head.
In one sentence

A 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

  1. struct Node holds value and next, a pointer to the following node.
  2. The loop inserts 3, 2, 1 at the front by setting the new node's next to the old head.
  3. The while (head) loop prints each value, saves next, then calls free on the node.
  4. 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::vector is 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

C
Run code →
main.c
#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;
}
Get a Run button for this example Result of running the code
1 2 3
C++
Run code →
main.cpp
#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';
}
Get a Run button for this example Result of running the code
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.

C

Structures and operations are usually separate and allocation is explicit.

C++

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.

Problem
free(head);
head = head->next;
Fix
Node *next = head->next;
free(head);
head = next;

Losing the rest of the list

Overwriting head without linking the old head leaks every node.

Problem
head = node;  /* node->next not set */
Fix
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