## leetcode02两数相加

2018年8月17日 发表评论 阅读评论

### 一、题目

```输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)

### 二、题目分析

```#!/usr/bin/env python
# coding=utf8
# ===============================================================================
#
#   Filename      ：02.py
#   Author        ：yangbk <itybku@139.com>
#   Create Time   ：2018-08-16 19:00
#   Description   ：
# ===============================================================================
l1 = [2,4,3]
l2 = [5,6,4]
a = ''
for i in l:
i = str(i)
a = a + i
return a
n3 = int(n1) + int(n2)
print n1,n2,n3
print  list(''.join(str(n3)[::-1]))
[root@localhost leetcode]# python 02.py
243 564 807
['7', '0', '8']```

### 四、功能实现

```class Solution(object):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
while flag or l1 or l2:
node = ListNode(flag)
if l1:
print node.val
node.val += l1.val
print node.val
print '---------'
l1 = l1.next
if l2:
node.val += l2.val
l2 = l2.next
print node.val
print '***********'
flag = node.val // 10
node.val %= 10
print flag,node.val
print '========'

```class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
pre = None
while cur:
h = cur
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return h
class Solution(object):
newl1=reverse(l1)
newl2=reverse(l2)
while newl1 or newl2 or flag:
node = ListNode(flag)
if newl1:
node.val += newl1.val
newl1 = newl1.next
if l2:
node.val += newl2.val
newl2 = newl2.next
flag = node.val // 10
node.val %= 10
l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)
l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)
p = Solution()

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

1. 2018年8月24日20:08 | #1

想知道你的python文件头部注释是自动生成的还是手动填写的啊

• 2018年8月28日10:24 | #2

vim配置自动生成的