Linked List Operations in DSA – Insert, Delete, Traverse

If you’re just beginning your journey into the world of Data Structures and Algorithms (commonly known as DSA), there’s a good chance you’ve heard about linked lists already.

They're everywhere - in textbooks, coding bootcamps, interviews, and even real-world applications. And while they’re one of the most fundamental data structures in computer science, it’s also true that they can seem a little intimidating at first glance.

Linked List Operations in DSA – Insert, Delete, Traverse

You might be wondering:

 “What’s a node?”

“Why do we need pointers?”

“Why not just use arrays for everything?”

These are all fair questions. In fact, almost every beginner asks them. Unlike arrays, where data is stored side by side in memory, linked lists are more flexible and dynamic. But this flexibility comes at the cost of understanding how memory management and pointers work - and that’s where most learners get stuck in the beginning.

But here’s the good news: once you wrap your head around the basics, linked lists actually become one of the clearest and most powerful tools in your programming toolkit. Whether you’re coding in C, C++, Python, or Java -understanding how linked lists work will open the door to other advanced data structures like stacks, queues, trees, and graphs.

And don’t worry - we’re not going to throw confusing jargon at you. In this blog, we’re going to walk you through everything step by step. Just like a friend explaining it to you after class or over a late-night study session.

We'll cover:

✅ What linked lists are

✅ Why they're useful

✅ The different types of linked lists

✅ And most importantly -how to insert, delete, and traverse nodes in a singly linked list

Whether you're preparing for college exams, gearing up for coding interviews, or just trying to understand the topic better for your own learning, you're in the right place.

So take a deep breath, grab your notebook or open your code editor, and let’s start this journey into the world of linked lists - together, one node at a time. 🧠💻✨

🌱 What is a Linked List?

Think of a linked list as a train, where each bogie (or node) contains two things:

1. The actual data

2. A pointer to the next bogie (the next node)

Unlike arrays, where all the data is stored in a contiguous block of memory, linked lists store each element separately and link them together using pointers. That’s why it’s called a “linked” list.

📦 Components of a Node

Copy Code

struct Node {

    int data;

    struct Node* next;

};

Here, each Node has an int type data and a pointer to the next node. Simple and powerful.

🔄 Why Use a Linked List Over an Array?

Arrays are great, but they have limitations:

You must define the size ahead of time.

Inserting or deleting an element requires shifting everything around.

With linked lists:

Dynamic memory allocation saves space.

Insertions and deletions are faster and more efficient.

This flexibility is the reason why linked lists are preferred in many real-life systems-especially where memory matters.

🛠️ Types of Linked Lists

Before getting into operations, let’s understand the different types of linked lists.

1. Singly Linked List – Each node points to the next one.

2. Doubly Linked List – Each node points to both next and previous.

3. Circular Linked List – The last node points back to the first.

For this article, we’ll stick to singly linked lists to keep things beginner-friendly.

🚀 Core Operations of a Linked List

Let’s focus on three major operations:

Insertion (adding elements)

Deletion (removing elements)

Traversal (going through the list)

We’ll explain each operation with simple language, practical examples, and clean code.

➕ Insertion in Linked Lists

Insertion is one of the most basic things you’ll do with linked lists. You can insert a node in three main ways:

1. At the beginning

2. In the middle (after a specific node)

3. At the end

1. Insert at the Beginning

Think of it as pushing a new book on top of a stack. It becomes the new first node.

Code:

Copy Code

void insertAtBeginning(struct Node** head, int new_data) {

    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

    new_node->data = new_data;

    new_node->next = *head;

    *head = new_node;

}

2. Insert After a Specific Node

Let’s say you want to insert a new friend into a group chat just after a specific friend. That’s this scenario.

Code:

Copy Code

void insertAfter(struct Node* prev_node, int new_data) {

    if (prev_node == NULL) return;

    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

    new_node->data = new_data;

    new_node->next = prev_node->next;

    prev_node->next = new_node;

}

3. Insert at the End

This is like adding someone to the end of a guest list.

Code:

Copy Code

void insertAtEnd(struct Node** head, int new_data) {

    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

    new_node->data = new_data;

    new_node->next = NULL;

    if (*head == NULL) {

        *head = new_node;

        return;

    }

    struct Node* last = *head;

    while (last->next != NULL) {

        last = last->next;

    }

    last->next = new_node;

}

❌ Deletion in Linked Lists

Deleting nodes is the opposite of inserting. Here’s how you do it.

1. Delete from the Beginning

Just like removing the first book from a stack.

Code:

Copy Code

void deleteFromBeginning(struct Node** head) {

    if (*head == NULL) return;

    struct Node* temp = *head;

    *head = (*head)->next;

    free(temp);

}

2. Delete at a Given Position

You want to remove someone who’s at a particular spot in a line.

Code:

Copy Code

void deleteAtPosition(struct Node** head, int position) {

    if (*head == NULL) return;

    struct Node* temp = *head;



    if (position == 0) {

        *head = temp->next;

        free(temp);

        return;

    }



    for (int i = 0; temp != NULL && i < position - 1; i++) {

        temp = temp->next;

    }



    if (temp == NULL || temp->next == NULL) return;



    struct Node* next = temp->next->next;

    free(temp->next);

    temp->next = next;

}

3. Delete from the End

Removing the last person in a train.

Code:

Copy Code

void deleteFromEnd(struct Node** head) {

    if (*head == NULL) return;

    if ((*head)->next == NULL) {

        free(*head);

        *head = NULL;

        return;

    }



    struct Node* temp = *head;

    while (temp->next->next != NULL) {

        temp = temp->next;

    }

    free(temp->next);

    temp->next = NULL;

}

🔍 Traversal – Printing All Nodes

To see what's inside a linked list, you need to go from the head to the last node.

Code:

Copy Code

void printList(struct Node* node) {

    while (node != NULL) {

        printf("%d -> ", node->data);

        node = node->next;

    }

    printf("NULL\n");

}

🧠 Real-Life Use Cases of Linked Lists

Music Playlists: Each song points to the next one.

Browser History: You can go back and forth .

Image Slideshows: Each image is linked to the next.

Even complex systems like OS memory allocation use linked lists!

🎓 Mistakes to Avoid

💥 Forgetting to free memory → causes memory leaks

💥 Misplacing pointers → leads to segmentation faults

💥 Not checking NULL → crashes your program

Practice writing small programs and break them intentionally to see what goes wrong.

❤️ Final Thoughts: Why Mastering Linked Lists Matters 

Let’s be honest-linked lists might not feel “fun” when you first hear about them. They’re not as flashy as React apps or as visual as a website design. But here’s the truth:

They are the roots of your data structure tree. 🌳

When you master the logic of linked lists:

You start thinking like a developer

You become comfortable with pointers

You build the mental map of how memory works

 

And that makes everything else easier-trees, stacks, queues, graphs… you name it.

Start small. Create a linked list of your favorite movies. Insert one when a new movie is released. Delete one you no longer love. Print your updated list.

Don’t just read code-type it, run it, break it, fix it. That’s how you learn.

If you ever feel stuck, remember: every developer has been confused by pointers. What matters is that you try again.

💡 Looking for Help? Uncodemy’s DSA course is built for beginners just like you. You’ll learn not just the code, but the thinking behind it. With real projects and expert guidance, you’ll grow faster and smarter.

So keep building. Your journey has just begun. The confidence you’ll gain from understanding linked lists will stay with you for every single project you ever build.

The world needs more coders like you. Curious. Determined. Willing to learn.

Let’s do this. One node at a time. 💻✨

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses