added the code to detect the loop and find the head and remove the loop in the list