πŸ”— Linked List Operations Explained Easily

harshavardhan Gharshavardhan G
3 min read

πŸ“Œ Introduction

After learning about different types of linked lists, it's time to master the most important part β€” Operations. Whether it’s inserting a node, deleting a node, or reversing a list β€” these are the most asked DSA interview questions and crucial concepts for real-world coding.

In this blog, you'll learn Linked List operations with code examples, diagrams, and tips.


πŸ”§ Common Linked List Operations

1️⃣ Insertion

  • At the Beginning

  • At the End

  • At a Specific Position

2️⃣ Deletion

  • From the Beginning

  • From the End

  • From a Specific Position

3️⃣ Traversal

  • Moving through the list from start to end.

4️⃣ Reversal

  • Reversing the entire linked list.

πŸ’» Insertion Operations (Singly Linked List in C++)

// Insert at the beginning
void insertAtBeginning(Node*& head, int val) {
    Node* newNode = new Node(val);
    newNode->next = head;
    head = newNode;
}

// Insert at the end
void insertAtEnd(Node*& head, int val) {
    Node* newNode = new Node(val);
    if (head == nullptr) {
        head = newNode;
        return;
    }
    Node* temp = head;
    while (temp->next != nullptr)
        temp = temp->next;
    temp->next = newNode;
}

// Insert at specific position
void insertAtPosition(Node*& head, int val, int pos) {
    if (pos == 1) {
        insertAtBeginning(head, val);
        return;
    }
    Node* newNode = new Node(val);
    Node* temp = head;
    for (int i = 1; i < pos - 1 && temp != nullptr; i++)
        temp = temp->next;
    if (temp == nullptr) return;
    newNode->next = temp->next;
    temp->next = newNode;
}

πŸ’» Deletion Operations (Singly Linked List in C++)

// Delete from beginning
void deleteFromBeginning(Node*& head) {
    if (head == nullptr) return;
    Node* temp = head;
    head = head->next;
    delete temp;
}

// Delete from end
void deleteFromEnd(Node*& head) {
    if (head == nullptr) return;
    if (head->next == nullptr) {
        delete head;
        head = nullptr;
        return;
    }
    Node* temp = head;
    while (temp->next->next != nullptr)
        temp = temp->next;
    delete temp->next;
    temp->next = nullptr;
}

// Delete from specific position
void deleteFromPosition(Node*& head, int pos) {
    if (pos == 1) {
        deleteFromBeginning(head);
        return;
    }
    Node* temp = head;
    for (int i = 1; i < pos - 1 && temp != nullptr; i++)
        temp = temp->next;
    if (temp == nullptr || temp->next == nullptr) return;
    Node* toDelete = temp->next;
    temp->next = temp->next->next;
    delete toDelete;
}

πŸ’» Reversing a Linked List

void reverseList(Node*& head) {
    Node* prev = nullptr;
    Node* current = head;
    Node* next = nullptr;
    while (current != nullptr) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    head = prev;
}

🎯 Practice Questions (Must Try)

  • Reverse a linked list

  • Insert in sorted linked list

  • Detect loop in linked list

  • Find middle node

  • Merge two sorted lists


βœ… Conclusion

Mastering insertion, deletion, and reversal operations forms the foundation of solving linked list problems in interviews and competitive programming. Once you're comfortable with these basics, you'll find it easier to implement advanced variations and optimize solutions.



Top 10 Linked List Interview Questions & Answers

0
Subscribe to my newsletter

Read articles from harshavardhan G directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

harshavardhan G
harshavardhan G