-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_2.py
81 lines (68 loc) · 2.62 KB
/
ex_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
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
70
71
72
73
74
75
76
77
78
79
80
81
import time
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""
!!! The fist Solution is 15% faster than Solution2 !!!
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
"""
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
"""
Return a ListNode of sum of every 2 numbers from l1 and l2
:param l1: the first linked list
:param l2: the second linked list
:return: ListNode of sum input numbers
"""
res_1 = str(l1.val)
res_2 = str(l2.val)
while l1.next:
l1 = l1.next
res_1 += str(l1.val)
while l2.next:
l2 = l2.next
res_2 += str(l2.val)
res = str(int(res_1[::-1]) + int(res_2[::-1]))
output = None
for i in res:
print(int(i), output)
output = ListNode(val=int(i), next=output)
return output
class Solution2:
"""
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
"""
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
"""
Return a ListNode of sum of every 2 numbers from l1 and l2
:param l1: the first linked list
:param l2: the second linked list
:return: ListNode of sum input numbers
"""
carry = 0
root = n = ListNode(0)
while l1 or l2 or carry:
val_1 = val_2 = 0
if l1:
val_1 = l1.val
l1 = l1.next
if l2:
val_2 = l2.val
l2 = l2.next
carry, val = divmod(val_1 + val_2 + carry, 10)
n.next = ListNode(val)
n = n.next
return root.next
if __name__ == "__main__":
start = time.time()
print(Solution().addTwoNumbers(ListNode(val=2, next=ListNode(val=4, next=ListNode(val=3, next=None))),
ListNode(val=5, next=ListNode(val=6, next=ListNode(val=4, next=None)))))
print(f'Total time: {time.time() - start}')