Last active
September 17, 2019 22:32
-
-
Save DataSolveProblems/09e1d725740467884d8d3db388d5790c to your computer and use it in GitHub Desktop.
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 ListNode(): | |
def __init__(self, x): | |
self.val = x | |
self.next = None | |
class Solution: | |
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: | |
prev = result = ListNode(None) | |
n = 0 | |
while l1 or l2 or n: | |
if l1: | |
n += l1.val | |
l1 = l1.next | |
if l2: | |
n += l2.val | |
l2 = l2.next | |
prev.next = ListNode(n % 10) | |
prev = prev.next | |
n //= 10 | |
return result.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment