forked from fantastic-Feifei/Leetcode_Solutions_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_test_version.py
More file actions
executable file
·103 lines (77 loc) · 2.11 KB
/
02_test_version.py
File metadata and controls
executable file
·103 lines (77 loc) · 2.11 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
[2,4,3]
[5,6,4,8,9]
def create_list(content):
head = ListNode(content[0])
ptr = head
for i in content[1:]:
ptr.next = ListNode(i)
ptr = ptr.next
return head
class Solution():
def add2numbers(self,l1:ListNode,l2:ListNode)->ListNode:
head = point = ListNode(0)
carry = 0
while(l1 or l2):
new_point = ListNode(0)
if(l1 == None):
sum_ = l2.val + carry
sum_
new_point.val = sum_ % 10
test=new_point.val
test
carry = sum_ // 10
carry
l2 = l2.next
elif(l2 == None):
sum_ = l1.val + carry
sum_
new_point.val = sum_ % 10
test=new_point.val
test
carry = sum_ // 10
carry
l1 = l1.next
else:
sum_ = l1.val + l2.val + carry
sum_
new_point.val = sum_ % 10
test=new_point.val
test
carry = sum_ // 10
carry
l1 = l1.next
l2 = l2.next
point.next = new_point
point = point.next
if(carry==1):
carry
new_point = ListNode(1)
test = new_point.val
test
point.next = new_point
return head.next
# def print_list(self,head:ListNode):
# p = head
# while(p != None):
# val = p.val
# val
# p = p.next
l1 = create_list([2,2,5])
l2 = create_list([5,6,5,9])
# temp2 = l1.val
# temp2 = l1.next.val
# temp2 = l1.next.next.val
# temp2
# temp = l2.val
# temp = l2.next.val
# temp = l2.next.next.val
# temp = l2.next.next.next.val
# temp = l2.next.next.next.next.val
# temp
ob = Solution()
print(ob.add2numbers(l1,l2))
# print(ob.print_list(l1))