FAQ: Linked Lists: Python - Linked List Implementation III

Hello peoples,

I don’t mean to sound critical of this lesson, I’m only trying to understand what is the point of it?
I do not have a computer science background, so I have absolutely no context into which to put this lesson.

Is there a broader concept this is trying to teach?

I feel like I’m missing the entire value of the exercise. I was able to complete it, but I gained no understanding from it. ( I learn best if I know the general context of what i’m supposed to use something for(what does this tool in the tool box do and how do I apply it.). I can hammer a square peg into a round hole with a spanner wrench, but it’s not a good idea, a good use of time; or a proper use of the tool.

“When one teaches, two learn” – I can’t remember who this quote is from

3 Likes

Nowadays, the linked list is a relatively esoteric data structure. You won’t really have much use for it unless you’re doing lower level programming. It’s usually used in contrast to an array(not to be confused with JS arrays which is equivalent to Python’s List). The data in an array has to be sitting next to each other in your system’s memory so it needs to have a set amount of memory allocated to it to fit the data. Linked lists on the other hand have pointers to the next data point which can be anywhere in system memory, so there doesn’t need to be any pre-allocated memory, it can grow or shrink with the data, which also makes it pretty efficient when adding/removing data. Why they’re teaching it here, it’s an important part of CS fundamentals. The knowledge learned here is important when working with more complex data structures and also opens more doors for what kind of projects you can work on in the future.

3 Likes

Whe you know that the file that you want to delete is b. You have to put None as b’s value for deleting the node so instead of havig:
a–>c you will have
a–>c<–b (bacause you alredy deleted the conection between a and b)

" Add an else clause. Within the else clause:

  • 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.)
    • When value_to_remove is found, adjust the links in the list so that current_node is linked to next_node.get_next_node() .
    • After you remove the node with a value of value_to_remove , make sure to set current_node to None so that you exit the loop."

i think the part say-" 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 " make mistakes, cause its not the way to write the condition.
just write somthing like- keep the loop until it meet the value to delete…
and let the user think how to write the condition

What is the point of most of the methods ?
I mean…

node.get_value() == node.value
node.get_next_node() == node.next_node

Why using methods for that?

2 Likes

I have the same question. It works just fine this way and it looks way clearer

1 Like

What I understood is that sometimes we don’t have full access to a program, being for example on “read-only”. In some cases using methods is the only way you have to access the data of your object.

Hey there.

Maybe its just me, but I’m finding the OOP and node lessons to be lacking in explanation and guidance for the exercises. It seems to go from “this is what a node is” to lets move throughout the list and delete nodes without any instruction on how to do so. I feel as though these lessons could be a little more thoroughly explained to prevent the confusion that is found in the preceding comments.

6 Likes

mate, totally agree with you!

4 Likes

Thanks so much! I had done the same except missed the second else statement so kept getting stuck on an infinite loop!

First we self.head_node = Node(value)

Yes at first a linked list object ‘ll’ is created by calling the LinkedList class, ll = LinkedList(5) , which itself calls the Node class, self.head_node = Node(value).
Inside the LinkedList class a node object is created called ll.head_node.
ll.head_node has access to Node’s functionality so ll.head_node.value has a value of 5 and ll.head_node.next_node has a value of None. When the insert_beginning() method is called via ll.insert_beginning(4) a new node object is created called new_node. Initially new_node has a value of 4 and a next_node of None.

new_node.set_next_node(self.head_node) sets the new_node objects next_node to that of the head_node.

self.head_node = new_node then assigns the head_node with new_node and the former head_node is now the second node in the LinkedList ‘ll’.

As far as your second question is concerned I’m not quite sure but it seems as though
self.head_node.set_next_node(self.head_node) sets the head_node’s next_node to itself.

1 Like

That’s a big part of it. While Python doesn’t exactly allow us to create private variables and methods like Java or C++, using getters and setters is still a way to control what a user or program has access to.
For example, you could put out an API of your linked list for other developers to use but you don’t want them to be able to change the values in the list. By not including a setter method, they wouldn’t know how to access the value variable to change it.

1 Like

I have the same question… Still don’t get it why we have to set current_node = None.

Probably better if you consider the intentions of what’s being done rather than some particular detail in the code. What should happen there? Is there repetition involved, when does that start when does that end?

When I hit the “Run” button, the program thinks forever. How can I overcome this problem or skip to the next stage?

Hello @method7269688012 and welcome to the Codecademy Forums!

Usually, this issue occurs when you have an infinite loop in your code. Double-check that you don’t have one.

I don’t think this is the case. I put print statements to check my program and the results print on the screen immediately (and correct), but the error message “Failed to test your code” appears after a very long time, and, needless to say, I cannot progress to the next stage. (see attached image)
Any suggestions?

I’m not sure what’s wrong then. Maybe trying copying your code, resetting the exercise, and running it again?

That helped! Thanks.
Amikam
2020-08-20T21:00:00Z

1 Like

Hi everyone, my question is regarding the garbage collection mechanism in python. What do I mean?
My understanding is that an object will be “garbage collected” if it is in isolation, that is, if it points to nothing AND nothing points to it.

Now, if you think about the head node of the list, it is an object that points to the next node (assume for a moment there is one) but is not pointed by any other node, right? We don’t expect the head node to be “garbage collected”.

Now, when we have the list a -> b -> c and we decide we want to move b, for example, we have the following states:
current_node.set_next_node(next_node.get_next_node()) - this will give us a -> c, and b -> c, and currNode -> a
current_node = None - this will give us a -> c, b -> c and currNode being null and not pointing to anything.
According with this memory picture, node b is not going to be “garbage collected” since it still points to node c. That is why, when I wrote my solution I had:

current_node.set_next_node(next_node.get_next_node())
next_node.set_next_node(None) #this line kills the b -> c, and now it is really "alone" - not pointing to anything, nor being pointed by anything
current_node = None

Is there something I don’t get about the garbage collection mechanism?

Thanks,
Eyal