Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Deletion in double linkedlist in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 75
    Comment on it

    Deletion of item from double linked list st known position takes O(1) complexity while deletion at some specified position takes O(n) time complexity.

    In deletion we basically removes the pointer of that item. First of all check the reference node value that would be equal to after value. Then set the link of ref node to temp node so that middle node could be remove.

    public void deleteAfterPos(T after){
        Node<T> tempNode = start;
        Node<T> refNode = null;
    
        while(true){
    
            if (tempNode == null){
                break;
            }else{
                if (tempNode.compareTo(after) == 0){
                    refNode = tempNode;
                    break;
                }
            }
    
            tempNode = tempNode.getNextReference();
        }
    
        if (refNode != null){
            tempNode = refNode.getNextReference().getNextReference();
            refNode.setNextReference(tempNode);
            tempNode.setPrevReference(refNode);
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: