class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
carry = 0
counter = 1
head = ListNode(None)
while l1 or l2 or carry:
sum = carry
if l1:
sum+= l1.val
l1 = l1.next
if l2:
sum+=l2.val
l2 = l2.next
digit = sum%10
carry = sum/10
if counter == 1:
head.val = digit
node = head
else:
node.next = ListNode(digit)
node = node.next
counter += 1
return head
100% Fastest Python Solution


This is simpler:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
head=ListNode(0)
temp=head
carry=0
while l1 or l2 or carry:
if l1:
carry+=l1.val
l1=l1.next
if l2:
carry+=l2.val
l2=l2.next
temp.next=ListNode(carry%10)
carry/=10
temp=temp.next
return head.next