Saturday, 31 January 2015

Deleting a node in a linked list when only a reference to only that very node is given

public static boolean delNode(LinkedlIstNode n )
{
    if ( n  ==  null || n.next == null )
    {
         //invalid linked list node
        return false ;
    }
    else
    {
        n = n.next.data ;
        n = n.next ;
        return true ;
    }   
}


A situation arises if given node n is last node . We can set last  node as a sentinal  of a dummy node.

No comments:

Post a Comment