# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
num1,num2=0,0
digit=0
while l1:
num1+=l1.val*(10**digit)
l1=l1.next
digit+=1
digit=0
while l2:
num2+=l2.val*(10**digit)
l2=l2.next
digit+=1
numsum=num1+num2
numsum,val=divmod(numsum,10)
keephead=head=ListNode(val)
while numsum:
numsum,val=divmod(numsum,10)
crt=ListNode(val)
head.next=crt
head=crt
return keephead
Thursday, September 18, 2014
Leetcode: Add Two Numbers @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment