Skip to main content

Easy Algorithms of Linked List

 

1. Algorithm to Insert a Node at the Beginning

📌 Adds a new node at the start of the linked list.

Algorithm:

1. Create a new node.
2. Assign data to the new node.
3. Make the new node point to the current head.
4. Update head to the new node.
5. End.

C Code:

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

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

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

2. Algorithm to Insert a Node at the End

📌 Adds a new node at the end of the linked list.

Algorithm:

1. Create a new node.
2. Assign data to the new node.
3. If the list is empty, make the new node the head.
4. Otherwise, traverse to the last node.
5. Make the last node’s next point to the new node.
6. End.

C Code:

void insertAtEnd(struct Node** head, int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    struct Node* temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
}

3. Algorithm to Delete a Node from the Beginning

📌 Removes the first node from the linked list.

Algorithm:

1. Check if the list is empty.
2. Store the head node in a temporary variable.
3. Move the head pointer to the next node.
4. Delete the temporary node.
5. End.

C Code:

void deleteAtBeginning(struct Node** head) {
    if (*head == NULL) return;
    
    struct Node* temp = *head;
    *head = (*head)->next;
    free(temp);
}

4. Algorithm to Delete a Node from the End

📌 Removes the last node from the linked list.

Algorithm:

1. Check if the list is empty.
2. If there is only one node, delete it and set head to NULL.
3. Traverse to the second last node.
4. Set the second last node’s next to NULL.
5. Delete the last node.
6. End.

C Code:

void deleteAtEnd(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;
}

5. Algorithm to Search for an Element

📌 Finds a specific element in the linked list.

Algorithm:

1. Start from the head node.
2. Traverse each node until the key is found or the list ends.
3. If found, return success.
4. If not found, return failure.
5. End.

C Code:

int search(struct Node* head, int key) {
    struct Node* current = head;
    while (current != NULL) {
        if (current->data == key) return 1;
        current = current->next;
    }
    return 0;
}

6. Algorithm to Traverse a Linked List

📌 Prints all elements in the linked list.

Algorithm:

1. Start from the head node.
2. Traverse each node while printing its data.
3. Stop when reaching the last node.
4. End.

C Code:

void display(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

Data structures are the foundation of efficient programming and problem-solving. Whether you are preparing for coding interviews, competitive programming, or software development, mastering data structures can significantly boost your logical thinking and efficiency.

🔹 What You’ll Learn:
✔️ Fundamentals of Data Structures – Arrays, Linked Lists, Stacks, Queues
✔️ Advanced Data Structures – Trees (BST, AVL, Red-Black), Graphs, Hashing
✔️ Algorithmic Techniques – Sorting, Searching, Divide & Conquer, Dynamic Programming
✔️ Code Implementations – C, C++, Java, Python tutorials with real-world examples
✔️ Interview Preparation – Top 100 Data Structure Questions with Solutions
✔️ Real-World Applications – How data structures power AI, ML, Big Data, and more

💡 Stay ahead in your programming journey with step-by-step explanations, visualizations, and hands-on practice problems.

Comments