List to linked-list Python

What operations does your linked list implement? Use those to build it from the python list.

Presumably youd start out with empty and append each value, no? And depending on how exactly your links work you might want to start either from the front or the back of the list.
And arguably thats something the linked lists __init__ should or at least could be doing [but then you would need a class for your linked list, you dont have that, you only have a class for a node you could consider a node to be a linked list [this is reasonable] but that would not be the nicest ever interface to expose the list with, so a containing type might be nice]

In particular theres a clash when creating a list, if a ListNode is a list, then creating a list would be done by calling ListNode but then how do you create a single node doesnt entirely add up

jaccobtw:

Can I just take a list and assign the whole thing as linked?

Uhm. keep in mind that your structure is different from that of a python list. If you keep the whole list then you do not have your structure, so no, that wouldnt be a linked list would it?

Keep in mind that the purpose of creating a linked list type is to expose operations. It is through them that you would do the conversion. And it is through them that you would use the list. And using a list probably involves creating lists.

Video liên quan

Chủ Đề