FAQ: Linked Lists: Python - Linked List Review


#1

This community-built FAQ covers the “Linked List Review” 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 List Review

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!


#3

Hi,

I have a question on the exercise in the Linked List Review page.
It’s about the insert_beginning method, which looks like this when implemented as instructed:

  def insert_beginning(self, new_value):
    print("Function that takes a value parameter")
    new_node = Node(new_value)
    new_node.set_next_node(self.head_node)
    self.head_node = new_node

I wanted to add an alternative insert_beginning method that directly takes a Node as argument:

  def insert_beginning(self, new_node):
    print("Function that takes a Node parameter")
    new_node.set_next_node(self.head_node)
    self.head_node = new_node

To my knowledge, this is method overloading, but for some reason, this doesn’t work.

I tried the following test code:

a = Node(1)
b = Node(2)
d = Node(3)
llist = LinkedList(5)
llist.insert_beginning(d)
llist.insert_beginning(b)
llist.insert_beginning(2)
llist.insert_beginning(a)

which gives some pretty strange results because the code only seems to use the original value method.

Thanks for clearing this up! :slight_smile:


#4

I myself am not very proficient in Python, and I only just completed this module.
But upon seeing your question, I tried your method.
It seems to work fine for me, it adds the node fine as a direct argument.

I’m thinking you could achieve what you want with modifying the original method instead, to accept Nodes as direct argument, with a “if isinstance(obj, class)” check
I made and tested the following:

def insert_beginning(self, new_value):
if isinstance(new_value, Node):
new_value.set_next_node(self.head_node)
self.head_node = new_value
else:
new_node = Node(new_value)
new_node.set_next_node(self.head_node)
self.head_node = new_node

This way it accepts both inputs. I hope this does not add to the confusion.