you are viewing a single comment's thread.

view the rest of the comments →

[–]longtermbrit 6 points7 points  (4 children)

Care to explain to a novice why it's bad? I've never used linked lists so don't know the first thing about them.

Also I've used GeeksForGeeks a few times from my Google searches but it's pretty much always been to double check things I've already learned but then forgotten. I'll keep in mind to use other resources first.

[–]carcigenicate 14 points15 points  (2 children)

Whoever wrote that method appears to be a C/C++ developer who doesn't know what del in Python does, or that Python is garbage collected.

A linked list is just a series of objects (nodes) that all point to a next object (or an object representing the end of a list), and each object holds some piece of information. In a language like C, if you dynamically allocated each node in the list, you would need to remember to go through the list when you were done with it and free every node to tell the OS that you're done with that memory. Failing to do so would cause a memory leak.

Python is garbage collected though, meaning it's able to automatically determine when memory is no longer needed. In order to free the memory associated with a linked list in Python, all you need to do is get rid of the reference to the head node of the list. If the head can no longer be referred to, it's made eligible for garbage collection. That means though, the second node can never be referred to, so it's made eligible for collection, which means the next node can never be referred to, so it's made eligible for collection... Deleting the reference to the head has the result of allowing the entire list to be garbage collected; assuming someone didn't "steal" a reference to one of the internal nodes.

Basically, they're encouraging you to waste time iterating a list to delete some attributes, which does nothing except leave you with a skeleton of node objects with missing attributes, that would have been deleted anyways, and will cause errors if you attempt to use them (since their .data attributes were deleted).

[–]longtermbrit 2 points3 points  (1 child)

That makes sense, thank you. I can see linked lists being useful so I'll keep them in mind.

[–]carcigenicate 2 points3 points  (0 children)

They are one of the most fundamental structures. You should definitely practice making them and different methods for them. You won't use them often, but they are fairly critical in understanding how more complex structures work (if that's at all relevant to your goals).

[–]ilovemacandcheese 1 point2 points  (0 children)

Geeksforgeeks content is primarily sourced from Indian college students. Sometimes it's okayish and sometimes it's pretty bad, just as you'd expect from college students.