-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo2 Add Two Numbers.py
69 lines (65 loc) · 1.76 KB
/
No2 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# class Solution(object):
# def addTwoNumbers(self, l1, l2):
# """
# :type l1: ListNode
# :type l2: ListNode
# :rtype: ListNode
# """
# if not l1 and not l2: return None
# if not l1: return l2
# if not l2: return l1
# order = 0
# len_l1 = self.count_len(l1)
# len_l2 = self.count_len(l2)
# if len_l1 < len_l2:
# head = l2
# else:
# head = l1
# node = head
# while l1 and l2:
# n = l1.val + l2.val + order
# if n >= 10:
# head.val = n - 10
# order = 1
# else:
# head.val = n + order
# order = 0
# head = head.next
# l1 = l1.next
# l2 = l2.next
# while l1 or l2:
# head.val = head.val + order
# order = 0
# head = head.next
# return node
# def count_len(self, l):
# count = 0
# while l:
# count += 1
# l = l.next
# return count
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = cur = ListNode(0)
carry = 0
while l1 or l2 or carry:
if l1:
carry += l1.val
l1 = l1.next
if l2:
carry += l2.val
l2 = l2.next
cur.next = ListNode(carry % 10)
cur = cur.next
carry //= 10
return dummy.next
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
if __name__ == "__main__":
solu = Solution()
l1 = ListNode(1)
l1.next = ListNode(8)
l2 = ListNode(0)
solu.addTwoNumbers(l1, l2)