Skip to content

Instantly share code, notes, and snippets.

@stochastic-thread
Created September 25, 2018 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stochastic-thread/284a280bf87fb2c3329264cfdcd7037e to your computer and use it in GitHub Desktop.
Save stochastic-thread/284a280bf87fb2c3329264cfdcd7037e to your computer and use it in GitHub Desktop.
Solution to LeetCode Algorithms #2 - "Add Two Numbers"
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
_sum = 0
dummy = ListNode(0)
current, carry = dummy, 0
while l1 or l2:
val = carry
if l1:
val += l1.val
l1 = l1.next
if l2:
val += l2.val
l2 = l2.next
carry, val = divmod(val, 10)
current.next = ListNode(val)
current = current.next
if carry == 1:
current.next = ListNode(1)
return dummy.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment