class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
v1 = v2 = 0
if l1:
v1 = l1.val
l1 = l1.next
if l2:
v2 = l2.val
l2 = l2.next
carry, val = divmod(v1+v2+carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
Clear python code, straight forward


I found this Stack Overflow to find out the answer.
n.next = n = ListNode(val) means first n.next = ListNode(val) then n point to the same address
n = n.next = ListNode(val) means first n = ListNode(val) , now the n is ListNode(val), then n.next point to the address ListNode(val) which means point to itself!!!
Hope this will help.

nice solution! However, I think the variable v1, v2 is unnecessary.
def addTwoNumbers(self, l1, l2): carry = 0; res = n = ListNode(0); while l1 or l2 or carry: if l1: carry += l1.val l1 = l1.next; if l2: carry += l2.val; l2 = l2.next; carry, val = divmod(carry, 10) n.next = n = ListNode(val); return res.next;




