FAQ: Linked Lists: Python - Linked Lists Implementation II


#1

This community-built FAQ covers the “Linked Lists Implementation II” exercise from the lesson “Linked Lists: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Linked Lists Implementation II

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Step 2 is confusing: You can use a while loop and Node 's next_node() method to traverse the list.

This is the solution version. How does the following work? I understand while loop using relation operators makes sense to me .

x=0
while x <3:
  print(x)
  x += 1

But I don’t understand the following:

 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()

How does this while loop check a True or False? What is it doing in these terms, specifically for linked lists?


#3

Alright, so visualizing this and hopefully trying to answer my own question:

Nodes actually look like this ( # is a place holder for value, and I’ll use different brackets to help with visualization):

blah_name = Node [ # , {#, (#, None) } ]

The head node contains all the nodes in it’s 2nd argument. So when :

current_node = current_node.get_next_node()

gets the None value, does that equate to false, thus stopping the while loop? But the if statement is checking for the None. Can someone try to break it down for me what the while loop is doing in this scenario?


#4

I think the next lesson gave me a clue!

Traverse the list until current_node.get_next_node().get_value() is the value_to_remove .

(Just like with stringify_list you can traverse the list using a while loop that checks whether current_node exists.)

It would still be nice to get a more detailed explanation of what while does through each check, especially at the end.


#5

So this is what I’m working with currently:

 def stringify_list(self):
    node_v = []
    nxt = self.head_node.get_next_node()
    node_v.append(str(self.head_node.get_value()))
    
    while nxt != None:
      node_v.append(str(nxt.get_value()))
      nxt = nxt.get_next_node()
      
    node_v = [n + "\n" for n in node_v]  
    
    return node_v

but every way I’ve tried, I can’t get the the list items to print on separate lines. I keep getting

['90\n', '5675\n', '70\n', '5\n']

I also get the following message at the bottom of the workspace:

'list' object has no attribute 'split'

I get this message wit or without an attempt to print on separate lines.
Any advice?