How to convert list to linked list?

Oh. It’s not map. They want you to carry the 1 to next node.

Well you COULD collect the digits, convert to int, split into digits, convert to linked list

… but I really don’t think that’s what they want you to do.
they’re hoping you will look at the first pair. add them. create a node. if there’s a 1 to carry, pass it along. step forward. add, including the carry, check if there’s a carry … and so on

this doesn’t involve conversion, it’s more like stepping through the list and producing a new list behind you as you go

…So you are supposed to look at the current pair
add them
make a node for the result

then call your own function with the tails of both lists, plus the carry that you need to be passing along, the result of this is the rest of the list

then return the node

They’re iterable but you have to implement iteration yourself by stepping through the structure of the list.
You only have the structure, there are no operations provided, so everything you do with such a list will involve picking it apart or building it up. There is no behaviour. Only structure.

I have to ask, how does one step through the structure

I’m using this:

def addition(n, m):
            return n + m
        
        return map(addition, l1, l2)

For the linked list used in that problem,

given a node, the next node is: node.next
regardless of which node you’re currently looking at, looking one deeper is done by following its next attribute… unless you’re at the last node because that’s not a ListNode
So iteration means repeatedly looking at next of the current node.

you cannot use the builtin map because map doesn’t know anything about the structure of your list

if you wanted to use map you would need to implement map for your list structure

but it’s not quite map, because you need to carry the 1

consider how you do addition between two large numbers using pen and paper.
you’d start at the least significant digit (beginning of the list)
add them
result is sum mod 10
carry is sum div 10

step forwards to next pair of digits
add them (and the carry)
result is sum mod 10
carry is sum div 10

note that you are building the result at the same time as you are iterating through the input, so you are therefore also going to have to iterate through your result while also building it. defining an append operation might therefore be useful to deal with that

1 Like

note: repeatedly move one step

while something:  # repeat
    current = current.next  # one step forwards

not: make many steps once

current.next.next.next  # not useful, no reason to specifically move 3 steps
1 Like
l1 = [1,2,3]
 current = l1.val
 current = l1.next

#current = ???

I’m not sure if this works. I get some weird value after its assigned to next

that’s not a linked list

next and val are very different things, don’t treat them the same.

Its just a representation. when u assign something to next, shouldnt it be equal to 2 (at least on the first go around)?

I think I understand the while loop implementation. How do you add the numbers into the links, if I’m understanding it correctly?

        current = l1
        current2 = l2
        while current.next != None:
            current = current.next
            current2 = current2.next
            amn = current.val + current2.val
            print(amn)

.I dont know how to add the numbers without having to create new nodes separate from the original ones. I cant assign linked nodes in a loop otherwise the previous nodes get deleted. so it seems I have to assign one by one

list1 = [7,0,8]
 count = 0
        ans = ListNode(list1[count])
        current3 = ans
        print(current3)
        while count <=2:
            count += 1
            current3.next = ListNode(list1[count])
            print(current3)
            if count == len(list1) -1:
                break
        return current3
        
#output
[7,8]

You should create new ones.
You can do it just fine in a loop.
Indeed, you will usually do things one at a time, because you would do something once, inside a loop, therefore many times.

There are no python lists involved, so you shouldn’t be using square brackets, not for list literals, because that’s the wrong kind of list, and not for indexing, because your list does not support that. Similarly, you can’t use len, so such behaivour is implemented for your list.

And you don’t need python lists. You already have a list type. Wouldn’t it be strange to require a second list type to use a list?

The only operations you have are:

read and write value of a node
read and write next node of a node
create node

and you can see this by looking at the code for the class that the problem statement shows you. There is almost nothing there. There is a constructor (__init__) and two fields. That’s it.

1 Like

If you dont use regular lists, then how do you store your addition? I have to store the addition then use that information to create new nodes - unless there is a way to directly transfer your math into a new node without being difficult. I’d have to use the assignment operator each time without a list to store data. Once they’re in the list, I can just pop them off in order.

Same way as when doing addition on pen and paper, you only handle one digit at a time.

Note that when you are doing it using pen and paper you are writing down the result while you carry out the operation.

Instead of writing it down, create a list node.


When you do it with pen and paper you are stepping through the two input lists, and building an output list.

This is how I added:

list1 = []
        current = l1
        current2 = l2
        amn = current.val + current2.val
        mod = 0
        zod = 0
        amn = zod + current.val + current2.val
        mod = amn%10
        if amn >= 10:
            list1.append(mod)
            zod = 1
        else:
            list1.append(amn)
            zod = 0
        while current.next != None:
            current = current.next
            current2 = current2.next
            amn = zod + current.val + current2.val
            mod = amn%10
            if amn >= 10:
                list1.append(mod)
                zod = 1
            else:
                list1.append(amn)
                zod = 0

so append to a linked list instead?

linked list does not have append

as a structure it supports adding things to it

…otherwise nobody would bother with them

right, but without doing it one by one by one

doing it once several times is how you do it many times

if you figure out how to do it once, then you have also figured out how to do it many times because you would do it once, in a loop