-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00002-add_two_numbers.py
64 lines (50 loc) · 1.42 KB
/
00002-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
# 2: Add Two Numbers
# https://leetcode.com/problems/add-two-numbers/
class ListNode:
def __init__(self, x: int=None):
self.val = x
self.next = None
class LinkedList:
def __init__(self) -> None:
self.head = None
def Input(self, inputs: list[int]):
for i in inputs:
node = ListNode(i)
if self.head:
tail = self.head
while tail.next != None: tail = tail.next
tail.next = node
else:
self.head = node
def Output(self, head: ListNode):
while (head != None):
print(head.val, "-> ", end='')
head = head.next
print("NULL")
class Solution:
# SOLUTION
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
head = ListNode(0)
t = head
c = 0
while c!=0 or l1!=None or l2!=None:
if l1!=None:
c += l1.val
l1 = l1.next
if l2!=None:
c += l2.val
l2 = l2.next
t.next = ListNode(c%10)
t = t.next
c //= 10
return head.next
if __name__ == "__main__":
o = Solution()
ll1, ll2 = LinkedList(), LinkedList()
# INPUT
li1, li2 = [2,4,3], [5,6,4]
ll1.Input(li1)
ll2.Input(li2)
# OUTPUT
result = o.addTwoNumbers(ll1.head, ll2.head)
ll1.Output(result)