Solution for problem 2-5 with singly linked list