How is my Code wrong?

I was completing The Linked Lists Implementation II exercise, The task Asks you to create a method that lists all values in the linked list of Nodes.

My Code:

  def stringify_list(self):
      x = self.head_node
      while x != None:
          print(x.get_value())
          x = x.get_next_node()

Their Code:

  def stringify_list(self):
    string_list = ""
    current_node = self.get_head_node()
    while current_node:
      if current_node.get_value() != None:
        string_list += str(current_node.get_value()) + "\n"
      current_node = current_node.get_next_node()
    return string_list

both give the output of:

90
5675
70
5
None

Did I do something wrong? my output was the same as the solution.

I’d hazard a guess that the output should not include None. Consider refactoring your code to avoid this spurious output. Perhaps it is not that issue but you may closer to the correct code once that has been performed.

2 Likes

You’re right! Thanks for the help.

1 Like