Optimizing a recursive double linked list delete node multiple times
<p>I wrote a recursive doubly linked C++ functions to delete all the nodes in a doubly linked list having a certain value. Is it possible to optimize this? Thank you. </p>

<pre><code>struct marynode {
    int value;
    marynode* next;
    marynode* prev;
};

void DoubleLinkedListDeleteMultiple(marynode*&amp; llist, int value){
    marynode* tmp = llist;

    if (llist == NULL)
        return;

    if (llist-&gt;value == value){
            DoubleLinkedListDeleteMultiple(llist-&gt;next, value); 
            if (llist-&gt;next == NULL){
                marynode* prev = llist-&gt;prev;
                delete llist;
                llist = NULL;
                llist = prev;
                if (llist)
                    llist-&gt;next = 0;
            }   
            else if (llist-&gt;prev == NULL){
                marynode* next = llist-&gt;next;
                delete llist;
                llist = NULL;
                llist = next;
                if (llist)
                    llist-&gt;prev = 0;
            }   
            else{
                marynode *x = llist;
                marynode  *mynext = x-&gt;next;
                while (x){
                    if (x-&gt;value == value){
                        marynode* clara = x-&gt;next;
                        marynode* zprev = x-&gt;prev;
                        delete x;
                        x = NULL;
                        x = clara;
                        llist = clara;
                        if (llist)
                            llist-&gt;prev = zprev;
                    }
                    else {
                        x = mynext;
                    }   
                    if (mynext){
                        mynext = mynext-&gt;next;
                    }
                }

            }
    }
    else{
            DoubleLinkedListDeleteMultiple(llist-&gt;next, value); 
    }
}
</code></pre>
