-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.add-two-numbers.py
44 lines (37 loc) · 1010 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
41
42
43
#
# @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
# 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]:
rem = 0
head = tmp = ListNode()
while l1 or l2:
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
sum_val = val1 + val2 + rem
rem = sum_val // 10
tmp.next = ListNode(sum_val % 10)
tmp = tmp.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if rem > 0:
tmp.next = ListNode(rem)
return head.next
# @lc code=end