-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.两数相加.py
32 lines (30 loc) · 824 Bytes
/
2.两数相加.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# @lc app=leetcode.cn id=2 lang=python
#
# [2] 两数相加
#
# @lc code=start
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
def add(a, b, carry):
if not (a or b):
return ListNode(1) if carry else None
a = a if a else ListNode(0)
b = b if b else ListNode(0)
val = a.val + b.val + carry
carry = 1 if val >= 10 else 0
a.val = val % 10
a.next = add(a.next, b.next, carry)
return a
return add(l1, l2, 0)
# @lc code=end