Given a list and a node to delete, use the following
algorithm:
// Are we deleting the head node?
if (node == list.head)
{
// Yes -- assign its next node as the new head
list.head = node.next
}
else // The node is not the head node
{
// Point to the head node
prev = list.head
// Traverse the list to locate the node that comes immediately
before the one we want to delete
while (prev.next != node)
{
prev = prev.next;
}
end while
// Assign the node's next node to the previous node's next
node
prev.next = node.next;
}
end if
// Before deleting the node, reset its next node
node.next = null;
// Now delete the node.
delete node;