Fixed deleteNode to consider beginning and end