Define toList function for Problem 1