Created
April 11, 2024 23:10
-
-
Save jesuscmadrigal/b9df1918b79ed75ba21d9875cdfd0a6b to your computer and use it in GitHub Desktop.
This file contains 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
#Add two numbers | |
class Solution: | |
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | |
dummy = ListNode() | |
tail, carry = dummy, 0 | |
while l1 or l2 or carry != 0: | |
num1 = l1.val if l1 else 0 | |
num2 = l2.val if l2 else 0 | |
nodeVal = num1 + num2 + carry | |
carry = nodeVal // 10 | |
nodeVal = nodeVal % 10 | |
tail.next = ListNode(nodeVal) | |
l1 = l1.next if l1 else None | |
l2 = l2.next if l2 else None | |
tail = tail.next | |
return dummy.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment