added code the free memory of linked list when no longer needed