C Creating a Linked List

Intermediate
6 min read
Updated: Nov 2026
1 example

Building a List Node by Node

To build a list, start with head = NULL and append each value. Appending to an empty list makes the new node the head; otherwise walk to the last node and link it. Keeping a tail pointer makes appending O(1) instead of O(n).

Remember
struct Node *head = NULL, *tail = NULL;
/* append v */
n = new_node(v, NULL);
if (!head) head = tail = n;
else { tail->next = n; tail = n; }

Cheat Sheet

Keep these facts handy while you work with Creating a Linked List in C.

TopicDetail
Empty listhead == NULL
Append with tailO(1)
Append without tailO(n)
AlwaysFree the list when finished

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Traversing a Linked List.

Example 1 — Create a list from an array

A tail pointer makes each append constant time.

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

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

static struct Node *new_node(int value, struct Node *next) {
    struct Node *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->data = value;
    n->next = next;
    return n;
}

static void print_list(const struct Node *p) {
    for (; p; p = p->next) printf("%d%s", p->data, p->next ? " " : "");
    printf("\n");
}

static void free_list(struct Node *p) {
    while (p) {
        struct Node *t = p->next;
        free(p);
        p = t;
    }
}

int main(void) {
    int values[] = {5, 10, 15};
    struct Node *head = NULL, *tail = NULL;

    for (int i = 0; i < 3; i++) {
        struct Node *n = new_node(values[i], NULL);
        if (!head) head = tail = n;
        else { tail->next = n; tail = n; }
    }

    print_list(head);
    free_list(head);
    return 0;
}

How It Works

  1. The first node becomes both head and tail.
  2. Later nodes are linked after tail, and tail advances.
  3. free_list releases every node at the end.

Edge Cases & Pitfalls

Check these before relying on Creating a Linked List in real programs.

tail

Forgetting to update tail

If tail is not advanced, later appends overwrite the same link.

head

Appending to NULL head

Walking head->next when head is NULL crashes. Handle the empty case first.

Key Takeaways

  • Empty case first.
  • Tail pointer for O(1) append.
  • Free at the end.

One line: To build a list, start with head = NULL and append each value.

Frequently Asked Questions

Define a node struct, start with head = NULL, and for each value allocate a node and link it to the end (or insert at the head).
Set head to the new node (and tail too, if you track it).
Continue with "C Traversing a Linked List" next. Visit every node to print, count, sum, or process the list. The C Linked Lists hub lists every topic in order.

Next: Traversing a Linked List

Visit every node to print, count, sum, or process the list.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful