adding a reverse function and implementing a end node