DEV Community

Discussion on: Singly Linked List: A Python Implementation

Collapse
ekand profile image
Erik Anderson Author

Thanks for the comment!
These are all good points. I was in fact planning on adding a List class, and may do that in the next post in the series.
Although, it's an interesting thought exercise to imagine implementing a linked list with only a Node class. For the reasons you've stated, I'm not sure if it's even possible, but it is interesting to think about.

Collapse
drvanon profile image
Robin Alexander Dorstijn

If you would just leave the node class like that, it would function like a type of graph where the connections are in one direction and no more is aware of the nodes that point to it.

Thread Thread
ekand profile image
Erik Anderson Author

Thank for you the comment!
That's an interesting observation about the graph. But there's one thing about that I don't understand. Wouldn't the code still need some mechanism to keep track of the first node? It seem to me that, without a pointer to the first node, that node would get lost and none of the other nodes could be accessed.
From what I've learned, it seems sensible to make that pointer to the first node be part of another class, perhaps a class called Graph. Is there another approach I could consider?

Thread Thread
brainfrz profile image
Terry W

In this case, whatever the calling code was would need to save the head pointer in the calling function and pass it around. This isn't exactly an antipattern as we do need to pass around both a string and its length in some languages like C, but it obviously isn't preferable.

Thread Thread
ekand profile image
Erik Anderson Author

Thanks for explaining that for me.