function to reverse linked list