Created
September 25, 2018 19:03
-
-
Save stochastic-thread/284a280bf87fb2c3329264cfdcd7037e to your computer and use it in GitHub Desktop.
Solution to LeetCode Algorithms #2 - "Add Two Numbers"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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