refactor code for linked list