-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2 addTwoNumbers.py
44 lines (43 loc) · 1.12 KB
/
2 addTwoNumbers.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
#!/usr/bin/python3
# Author fengru
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
headNode = ListNode(0)
point = headNode
sum = 0
while (l1 != None) or (l2 != None) :
if(l1 != None) :
sum = sum + l1.val
l1 = l1.next
if(l2 != None) :
sum = sum + l2.val
l2 = l2.next
newNode = ListNode(sum % 10)
point.next = newNode
point = point.next
sum = sum//10
if sum > 0 :
newNode = ListNode(sum)
point.next = newNode
return headNode.next
l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)
sol = Solution()
l3 = sol.addTwoNumbers(l1, l2)
print(l3.val)
print(l3.next.val)
print(l3.next.next.val)