-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.py
More file actions
36 lines (29 loc) · 813 Bytes
/
AddTwoNumbers.py
File metadata and controls
36 lines (29 loc) · 813 Bytes
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
"""
https://leetcode.com/problems/add-two-numbers/description/
#implementation #linked-list #math
"""
class listnode:
def __init__(self, x):
self.val = x
self.next = none
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
carry = 0
dummyHead = ListNode(0)
iterNode = dummyHead
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry +=l2.val
l2 = l2.next
iterNode.next = ListNode(carry % 10)
iterNode = iterNode.next
carry //= 10
return dummyHead.next