Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • traverse and reverse double linked list in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 446
    Comment on it

    Traverse and reverse a linked list takes time complexity of O(n).

    Traverse a double linked list requires only node next reference until it reach to trail or null value :

    public void traverseLinkedList(){
    
        Node<T> tempNode = start;
    
        while (tempNode != null){
            tempNode = tempNode.getNextReference();
    
            Log.e("value", "" + tempNode.getValue());
        }
    
    }
    
    Reverse a double linked list :
    
    public Node<T> reverseLinkedList(){
    
        Node<T> temp = null;
        Node<T> current = start;
    
        while (current.getNextReference() != null){
    
            if (current == null){
                break;
            }else{
                temp = current.getNextReference();
                current.setNextReference(current.getPrevReference());
                current.setPrevReference(temp);
                current = temp;
            }
    
        }
        return current;
    
    }

 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: