-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAddTwoNumbers.py
More file actions
34 lines (30 loc) · 844 Bytes
/
Copy pathAddTwoNumbers.py
File metadata and controls
34 lines (30 loc) · 844 Bytes
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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
c = 0
res = []
while(l1 or l2):
s = 0 + c
if l1:
s += int(l1.val)
l1 = l1.next
if l2:
s += int(l2.val)
l2 = l2.next
print(s)
resa = s % 10
res.append(resa)
c = s // 10
if(c!=0):
res.append(c)
l3 = ListNode(0)
head = l3
for i in range(0, len(res)):
lt = res[i]
l3.next = ListNode(lt)
l3 = l3.next
return head.next