0Pricing
C Academy · Lesson

Singly Linked Lists

Nodes and pointers.

Singly Linked Lists is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a linked list?

A linked list is a chain of small structures called nodes. Each node holds a value and a pointer to the next node.

Unlike arrays, the elements need not be contiguous in memory, and the list can grow or shrink easily.

#include <stdio.h>

struct Node {
    int value;
    struct Node *next;
};

int main(void) {
    printf("A node holds a value and a next pointer\n");
    return 0;
}

Defining a node

The node struct contains the data plus a struct Node *next pointing to the following node.

The pointer type refers to the same struct, which is how the chain links together.

#include <stdio.h>

struct Node {
    int value;
    struct Node *next;
};

int main(void) {
    struct Node n;
    n.value = 42;
    n.next = NULL;
    printf("value=%d, next is NULL: %d\n", n.value, n.next == NULL);
    return 0;
}

The head pointer

A list is identified by a single pointer to its first node, called the head.

An empty list is simply a head equal to NULL.

#include <stdio.h>

struct Node { int value; struct Node *next; };

int main(void) {
    struct Node *head = NULL;
    printf("List is empty: %d\n", head == NULL);
    return 0;
}

Allocating a node

Nodes are usually created on the heap with malloc so they outlive the function that makes them.

Always check the return value and remember to free nodes later.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };

int main(void) {
    struct Node *n = malloc(sizeof(struct Node));
    n->value = 7;
    n->next = NULL;
    printf("%d\n", n->value);
    free(n);
    return 0;
}

Arrow operator

When you have a pointer to a struct, use -> to access members. n->value means the same as (*n).value.

You will use the arrow operator constantly with linked lists.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };

int main(void) {
    struct Node *n = malloc(sizeof(struct Node));
    n->value = 99;
    printf("%d\n", n->value);
    free(n);
    return 0;
}

Linking two nodes

To connect nodes, set the next of the first to point at the second. The last node's next stays NULL to mark the end.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };

int main(void) {
    struct Node *a = malloc(sizeof(struct Node));
    struct Node *b = malloc(sizeof(struct Node));
    a->value = 1; a->next = b;
    b->value = 2; b->next = NULL;
    printf("%d -> %d\n", a->value, a->next->value);
    free(a); free(b);
    return 0;
}

A helper to create nodes

Repeated allocation is tedious, so wrap it in a helper function that allocates, initializes, and returns a new node.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };

struct Node *make(int v) {
    struct Node *n = malloc(sizeof(struct Node));
    n->value = v;
    n->next = NULL;
    return n;
}

int main(void) {
    struct Node *n = make(5);
    printf("%d\n", n->value);
    free(n);
    return 0;
}

Building a small list

Using the helper, build a three-node list 1 -> 2 -> 3 by chaining the next pointers.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}

int main(void) {
    struct Node *head = make(1);
    head->next = make(2);
    head->next->next = make(3);
    printf("%d %d %d\n", head->value, head->next->value, head->next->next->value);
    return 0;
}

Printing the list

To print every value, start at the head and follow next pointers until you reach NULL.

This walking pattern is the foundation of nearly all list operations.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}

int main(void) {
    struct Node *head = make(1);
    head->next = make(2);
    for (struct Node *p = head; p; p = p->next)
        printf("%d ", p->value);
    printf("\n");
    return 0;
}

Arrays vs linked lists

Arrays give fast index access but fixed size. Linked lists give easy insertion and removal but slower access (you must walk to reach an element).

Choose based on which operations dominate your program.

#include <stdio.h>

int main(void) {
    printf("Array: O(1) index, costly resize\n");
    printf("List:  O(n) index, cheap insert/delete\n");
    return 0;
}

Freeing the whole list

Every malloc'd node must be freed. Walk the list, but save the next pointer before freeing each node, or you lose the rest of the chain.

#include <stdio.h>
#include <stdlib.h>

struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}

int main(void) {
    struct Node *head = make(1);
    head->next = make(2);
    struct Node *p = head;
    while (p) {
        struct Node *nxt = p->next;
        free(p);
        p = nxt;
    }
    printf("freed all nodes\n");
    return 0;
}

Quick Check

Test your understanding of linked list structure.

Recap

You learned the basics of singly linked lists:

  • A node holds a value and a next pointer; the head points to the first node.
  • Allocate nodes with malloc and access members with ->.
  • The last node's next is NULL; traverse by following pointers.
  • Always free every node, saving next before freeing.

Frequently asked questions

Is the “Singly Linked Lists” lesson free?

Yes — the full text of “Singly Linked Lists” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Singly Linked Lists”?

Nodes and pointers. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Singly Linked Lists” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Singly Linked Lists
  2. Insertion and Deletion
  3. Traversal and Search
  4. Doubly Linked Lists
← Back to C Academy