# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
node = temp = ListNode(-1)
carry = 0
while l1 or l2 or carry:
value = (l1 and l1.val or 0) + (l2 and l2.val or 0) + carry
carry = value // 10
node.next = ListNode(value % 10)
node = node.next
l1 = l1 and l1.next
l2 = l2 and l2.next
return temp.next
leetcode——2. 两数相加
最新推荐文章于 2024-05-03 18:06:08 发布