See Below.
Here the complete method:
public class OrderedLinkedList
{
private Node headPtr = null;
. . . .
// Delete the first node that contains value.
// if a node was deleted, return true
// if the value was not found, return false
//
public boolean deleteValue(int value)
{
// CASE I: Empty list
if ( headPtr == null ) return false;
// CASE II: Delete first node
// value is in first node, unlink it, return true
if ( headPtr.getValue()==value )
{
headPtr = headPtr.getNext();
return true;
}
// CASE III and CASE IV: delete middle or last node.
// Search for a node that contains value.
// Might be last one.
Node current = headPtr;
Node next = headPtr.getNext();
while ( next!=null && next.getValue() < value )
{
current = next; // advance current pointer
next = next.getNext(); // advance the next pointer
}
// if value is found, unlink it, return true
if ( next!=null && next.getValue() == value )
{
current.setNext( next.getNext() );
return true;
}
// value not in the list: return false
return false;
}
. . .
}
Would this method work for an unordered linked list?