-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.add-two-numbers.py
40 lines (33 loc) · 987 Bytes
/
2.add-two-numbers.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
32
33
34
35
36
37
38
39
40
#
# @lc app=leetcode id=2 lang=python3
#
# [2] Add Two Numbers
#
# @lc code=start
# 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: Optional[ListNode], l2: Optional[ListNode]
) -> Optional[ListNode]:
ans = ListNode(0)
start = ans
while (l1 or l2) and ans:
a = l1.val if l1 else 0
b = l2.val if l2 else 0
val = a + b + ans.val
if val >= 10:
ans.val = val - 10
ans.next = ListNode(1)
else:
ans.val = val
l1 = l1.next if (l1 and l1.next) else None
l2 = l2.next if (l2 and l2.next) else None
if (l1 or l2) and ans.next is None:
ans.next = ListNode(0)
ans = ans.next if (l1 or l2) else None
return start
# @lc code=end