8000 add AddTwoNumbers · jiaxinhuang/python-test-code@593503f · GitHub
[go: up one dir, main page]

Skip to content

Commit 593503f

Browse files
committed
add AddTwoNumbers
1 parent 39c78a7 commit 593503f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

leetcode/AddTwoNumbers.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#-*- coding:utf-8 -*-
2+
'''
3+
Created on 2017年5月12日
4+
5+
@author: huangjiaxin
6+
'''
7+
#Definition for singly-linked list.
8+
class ListNode(object):
9+
def __init__(self, x):
10+
self.val = x
11+
self.next = None
12+
13+
class Solution(object):
14+
def addTwoNumbers(self, l1, l2):
15+
"""
16+
:type l1: ListNode
17+
:type l2: ListNode
18+
:rtype: ListNode
19+
"""
20+
l3 = ListNode(0)
21+
root = l3
22+
val = 0
23+
while l1 or l2 or val:
24+
tempval = 0
25+
if l1:
26+
tempval += l1.val
27+
l1 = l1.next
28+
if l2:
29+
tempval += l2.val
30+
l2 = l2.next
31+
val, nowval = divmod(tempval + val, 10)
32+
tempList = ListNode(nowval)
33+
l3.next = tempList
34+
l3 = l3.next
35+
return root.next

0 commit comments

Comments
 (0)
0