C Academy · Lesson

Linked Lists

Learn how linked lists work and how to implement operations like insertion and deletion.

Lesson 1 of 39 steps

Linked Lists is a free C Academy lesson on CoddyKit. This is lesson 1 of 3. 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, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Linked Lists in C

A linked list is a dynamic data structure where elements (nodes) are connected using pointers.

In this lesson, you will learn:

  • How linked lists work.
  • How to insert and delete nodes in a linked list.
  • The advantages of linked lists over arrays.
Linked Lists — illustration 1

Example: Defining a Linked List Node

In C, a linked list node is defined using a struct with a data field and a pointer to the next node.

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

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

int main() {
    struct Node *head = NULL;
    return 0;
}

Inserting a Node at the Beginning

To insert a new node at the beginning of a linked list:

  • Create a new node.
  • Set its next pointer to the current head.
  • Update the head pointer.

Example: Inserting a Node at the Beginning

This program inserts a new node at the beginning of a linked list.

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

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

void insertAtBeginning(struct Node **head, int newData) {
    struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = newData;
    newNode->next = *head;
    *head = newNode;
}

int main() {
    struct Node *head = NULL;
    insertAtBeginning(&head, 10);
    printf("Inserted: %d\n", head->data);
    return 0;
}

Deleting a Node

To delete a node from a linked list:

  • Find the node to be deleted.
  • Update the previous node’s next pointer.
  • Free the memory of the deleted node.

Example: Deleting a Node

This program deletes a node from a linked list.

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

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

void deleteNode(struct Node **head, int key) {
    struct Node *temp = *head, *prev;
    if (temp != NULL && temp->data == key) {
        *head = temp->next;
        free(temp);
        return;
    }
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return;
    prev->next = temp->next;
    free(temp);
}

int main() {
    struct Node *head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 10;
    head->next = NULL;
    deleteNode(&head, 10);
    return 0;
}

Advantages of Linked Lists

Linked lists have several advantages over arrays:

  • Dynamic size (no need to specify size beforehand).
  • Efficient insertions and deletions.
  • No memory wastage due to fixed sizes.

Summary

In this lesson, you learned:

  • What linked lists are and how they work.
  • How to insert and delete nodes.
  • The advantages of linked lists over arrays.

Next, we will explore stacks and queues in C!

Linked Lists — illustration 9
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Linked Lists” lesson free?

Yes — the full text of “Linked Lists” is free to read here on the web. 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. The C Academy course includes 3 lessons in total.

What will I learn in “Linked Lists”?

Learn how linked lists work and how to implement operations like insertion and deletion. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “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. Linked Lists
  2. Stacks and Queues
  3. Trees and Graphs
← Back to C Academy