help i need to search and delete a node from a link list
but i am not sure of the algorithm
can someone please help me with the algorithm and concept
need example thanks..

Dani AI

Generated

Building on 's question and the hints from and , here is a concise, practical approach for searching and deleting nodes in a singly linked list, plus a few pitfalls to watch for.

  • Search (by value): iterate from head until a node with matching value is found; return the node (or index) or null if not found. Time: O(n).
  • Delete first occurrence (by value): handle the head specially (it may be the one to remove). Otherwise keep two pointers, prev and curr; when curr->val == key, do prev->next = curr->next; delete curr; and return the (possibly updated) head.
  • Delete all occurrences: use a dummy/sentinel node pointing at head to simplify head deletions; iterate and remove every matching node by reconnecting prev->next.
  • Delete by node pointer: if only a pointer to the node is available and it is not the tail, copy next node data into it and delete the next node; otherwise traversal to find the previous node is required.

Example C++ (singly linked list, delete-first and delete-all):

struct Node {
    int val;
    Node* next;
    Node(int v): val(v), next(nullptr) {}
};

Node* deleteFirst(Node* head, int key) {
    if (!head) return nullptr;
    if (head->val == key) {
        Node* n = head->next;
        delete head;
        return n;
    }
    Node* prev = head;
    Node* curr = head->next;
    while (curr) {
        if (curr->val == key) {
            prev->next = curr->next;
            delete curr;
            return head;
        }
        prev = curr;
        curr = curr->next;
    }
    return head; // not found
}

Node* deleteAll(Node* head, int key) {
    Node dummy(0);
    dummy.next = head;
    Node* prev = &dummy;
    Node* curr = head;
    while (curr) {
        if (curr->val == key) {
            prev->next = curr->next;
            delete curr;
            curr = prev->next;
        } else {
            prev = curr;
            curr = curr->next;
        }
    }
    return dummy.next;
}

Cautions: always check for nullptr, free memory exactly once, update any stored tail or size counters, and test these routines on edge cases (empty list, single-node list, head/tail deletions). Consider unique_ptr for safer memory management in modern C++.

Recommended Answers

All 3 Replies

help i need to search and delete a node from a link list
but i am not sure of the algorithm
can someone please help me with the algorithm and concept
need example thanks..

Draw a picture of a linked list on paper. Then write down the steps
you feel is needed to find an element. The same for deleting an element.

commented: Nice +19

a search needs a for loop which keeps on moving the pointer forward and if it find it returns true.

similar thing for a general delete I would think. You would have to traverse entire list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.