add end nodes (functional version)