FAQ: Linked Lists: Python - Linked List Implementation III

Hi @stewiegreen, and welcome to the Codecademy Forums!

In your posted code, the header line of remove_node is not indented at all. In that condition, remove_node would be interpreted as a global function, rather than as a method belonging to any class. In your actual code, is that line indented?

yes, in my code it is indented.

here is the whole class and it still gets this error:
“Did you remove your remove_node() method accidentally?”

class LinkedList:
  def __init__(self, value=None):
    self.head_node = Node(value)
  
  def get_head_node(self):
    return self.head_node
  
  def insert_beginning(self, new_value):
    new_node = Node(new_value)
    new_node.set_next_node(self.head_node)
    self.head_node = new_node
  
  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
  
  def remove_node(self, value_to_remove):
    current_node = self.get_head_node
    if current_node.get_value() == value_to_remove:
      self.head_node = current_node.get_next_node()
    else:
      pass

This line in the remove_node method does not perform its designated task, because the get_head_node method is not actually being called:

    current_node = self.get_head_node

The intended method call must include parentheses, even though there are no arguments to be passed.

EDIT (September 15, 2019):

The message regarding removing your remove_node() method accidentally is actually from Codecademy’s submission correctness test (SCT) for this exercise, rather than from the Python interpreter. Though you did not remove that method, the SCT did not find a properly functioning remove_node() method, and issued a somewhat misleading message about that.

yup, that solved it. thanks

1 Like

Is there any advantage to using the getter and setter methods over just referencing the class’s arguments?
For example, if you had to set a new “next_node” is there a difference between doing it like this:
new_next_node = my_node.next_node
or like this:
new_next_node = my_node.get_next_node()

Might just be a style thing, but just wondering!
Thanks!

I am afraid that I am seeing the same thing that Stewie saw…identical.

I pasted my code into a vi windows and “set list” to see any tab char…none.

Odd, as it ran fine previously…

Is there not some pretty printer we could use to regularize our code for your processor?

I hate to grab the solution, but am stuck. :0)

Holy Mama! …missing ()'s

Man, but a more suggestive error message would be appreciated!

All I can add folks is that, if this happens to you, you should just walk back to before the error and just add back the offending code a bit at a time.

LOL

All I can say is that I’m glad I’m not the only one this happens to :wink:

1 Like

com
may please anyone help ?
i just can’t imagine how the head node still the first node didn’t i here changed it every time in the while loop to reach the tail node so now current node is the tail node
and at the first of the method i initial the current_node to the head_node
so now head_node = current_node = tail_node
HOW CAN THE HEAD NODE STILL THE FIRST ONE

You’re not changing the list you’re traversing it.

If you’re at a node, how do you get to the next one?
Repeat until you reach the end.

Also, I would argue that your while and if are testing the same thing, or almost, because it’s wrong to test the value since the value might really be supposed to be None if it’s a list of None’s

1 Like

i know i have to do that to iterate through the list , but may you please explain this
lets say our list is 2>3>4>None and current_node is equal to the head node equal to 2 so now when i write current_node = current_node.next_node isn’t that mean current_node now become 3
and in python current_node = self.head mean they are the same now and any change for one will happen to the other how the self.head still 2 ??

setting a variable to a value doesn’t make the new value become the same value as the old value

a = 3
a = 2

3 and 2 are not the same value, 3 didn’t become 2, 3 is 3

to change a value you would have to change the value itself. assignment doesn’t change anything

1 Like

i got it really thank you :heart_eyes:
but here current_node = self.head i set an object to another object so what i understand that they now are the same i hope you understand my question from this code Screenshot_2

no you’re not setting an object
you’re setting a variable to refer to an object
you have two variables and they are referring to the same object

1 Like

Please accept my deepest thanks
felling happy now! :joy::heart_eyes:

---
variables
---
(name = location of object)
a = 3473  # after a = "hello"
b = 3474  # after b = " world"


---
memory
---
(location = data)
3473      = "hello"
3474      = " world"
1 Like

so this lineif current_node.get_value() != None is unnecessary? because why should i prevent the method from printing the list if it’s list of None’s


and i really understood that self.head and current_node is just a pointers referring to the same object … so how the pointer works then ?
i mean if any changes to the pointer will not change the object it self , its just i made the pointer to refer to the next node but why in adding a node to the end method … after i iterate the list to reach the tail i have to set the current_node (tail) to the new_node
and now when i set it ,it will change the tail to the new node
how can pointer don’t change the object while iterating the list but when i reached my goal i can easily set it to any value i want using pointer too !!

i think there is smth wrong in my understand

If you draw a list on paper and point at the first cell, then move it to the next step repeatedly, then your finger is doing the same thing as your variable.

If you’re not moving your finger then you won’t leave your current location.
If you have a pen and write something in the cell then it’ll change.
Moving your finger doesn’t cause anything to change on the paper.

2 Likes

I can’t move past step 2 of this exercise.

What am I doing wrong?

This is the error I’m getting which, according to the test file, is an AttributeError:

Did you remove your remove_node() method accidentally?

And here is my code:

# getters and setters removed because they're redundant.
class Node:
  def __init__(self, value, next_node=None):
    self.value = value
    self.next_node = next_node

class LinkedList:
  def __init__(self, value=None):
    self.head_node = Node(value)

  def insert_beginning(self, new_value):
    new_node = Node(new_value)
    new_node.next_node = self.head_node
    self.head_node = new_node
    
  def stringify_list(self):
    string_list = ''
    current_node = self.head_node
    while current_node:
      string_list += str(current_node.value) + '\n'
      current_node = current_node.next_node
    return string_list
  
  def remove_node(self, value_to_remove):
    current_node = self.head_node
    if current_node.value == value_to_remove:
      self.head_node = current_node.next_node

there is no error in your code the code will run
but your remove_node method will not work