简体   繁体   中英

Double Linked List delete

A double linked list delete method but it is not deleting the number provided to it.

Node node = head;
tail = head;
node = node.next;
while (tail != null) {
    tail.next = node;
    node.previous = tail;
    if (tail.data == data)
        break;
    tail = tail.next;
    node = node.next;
}
tail.previous = node.previous;
node.previous = tail.previous;

Try the following logic

    node = head;
    while (node != null) {
        if (node.data == data){
            //now you want to delete node
            if(node.previous != null) node.previous.next = node.next;
            if(node.next != null) node.next.previous = node.previous;
            return true; //found 
        }        
        node = node.next;
    }
    return false; //not found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM