 # How to convert list to linked list?

A list and a linked list often look exactly the same, but how can I convert a list to a liked list?

if I have [8,0,5] how do I convert it to [8,0,5] (8 -> 0 -> 5)

``````class ListNode:
def __init__(self, x):
self.val = x
self.next = None
``````

I know I can use to assignment operator to assign nodes but I need a higher order operation:

``````list_k = [8,0,5]

``````

But is there a way I can make a linked list so I dont have to assign each node one by one? Can I just take a list and assign the whole thing as linked?

Thanks

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

Presumably you’d 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 that’s something the linked list’s `__init__` should or at least could be doing (but then you would need a class for your linked list, you don’t 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 there’s 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 … doesn’t entirely add up

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 wouldn’t 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.

The problem is I cant append each value because the given input has a random number of values. So, I need something that can interpret the number of given values and then assign them together to form a linked list. Here is the problem if you wanna see: https://leetcode.com/problems/add-two-numbers/submissions/

I hope my code is not an obstruction

Uhm. Why would that matter? Append them all. All means each one regardless of amount.

If you make a function that handles a list
then it would be VERY weird to say. yeah it definitely has 5 elements.
You would make use of whatever length the input has, that’s not particularly special.

If a given input has 3 values, I can make code that assigns 3

If it has 4 values, I can make code that assigns 4, but no longer assigns 3 unless I make a ton of conditionals

if the code has 5,000 values, then that would take forever based on my knowledge

Yes but it must account for all inputs simultaneously

Did you know a loop will make one repetition for each value in a list regardless of the length of it?

Right? that’s what a loop does.

If you assumed a specific length that would be incredibly wrong, the idea of a list doesn’t have a specific length. Each instance of a list has some specific length and whatever operations you’d ever do on lists would look at the length of the specific list it is currently dealing with and act accordingly.

Look at the sum function.
It sums the numbers of a list.
Regardless of how many numbers that is.
That’s not weird, is it?

My problem is that I have to assign one node at a time regardless of the list length.

There’s repetition in that, so put the repeated part in a loop.

``````While whatever:
add a value to the tail  # also known as append
``````

You can do this with a regular list as well, it too has an append operation.

list.append

so you would start out with empty:

``````empty = []
``````

Then you’d have some collection of values

``````stuff = ['teddy', 'banana', 'quilt']
``````

each of which should be dropped into the list one at a time

``````for thing in stuff:
empty.append(thing)

# note, no mention of 3
``````

But dont u have to assign each node?

``````list_k = [8,0,5]

``````

If you have a list, would you create a variable for each value in the list?
No, that’s why you have a list, a container for all of them.

Assignment doesn’t do anything other than creating a variable, there’s no other effect.

I get the append to a list stuff, its just the linked list notation thats difficult

Make an empty list.

figure out how to append a value for it.

Given a list and a value, obtain the list which is the original list with the value included.

append until done.

a python list is not a linked list, no.
a python list doesn’t even expose its structure, it only exposes operations.
you can do things like append, iterate… and so on.

so make yourself an append function, then use that function to repeatedly append to a list

Btw the exercise you linked to doesn’t give you a python list so you shouldn’t be converting.

You should be applying + between two lists, this is `map`

``````>>> list(map(add, [1,2,3], [20, 30, 40]))
[21, 32, 43]
``````

but all those lists are linked lists

``````def_append(list):
list.append(x)

list_L = [1,2,3,4,,6,7,8,9]

append(list_L)

#output

[1,2,3,4,5,6,7,8,9,x]

``````