题目:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

代码:

该题目其实就是链表中的数字取出来,然后相加,不过注意高位数字在后面,需要倒过来。比如题目例子中就是要:342+465=807,之后把807每一位从小到大记录在一个链表里。

于是,我用了最常规的办法,不过也是解决了问题的:

#coding:utf-8
# Definition for singly-linked list.
class ListNode(object):
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
"""
if not l1 and not l2:return
#取出链表中的数字存入数组
arr1,arr2=[],[]
while l1:
arr1.append(l1.val)
l1 = l1.next
while l2:
arr2.append(l2.val)
l2 = l2.next
#倒序
arr1.reverse()
arr2.reverse()
#print (arr1,arr2)
#组成数字
num1,num2 = 0,0
for i in arr1:
num1 = num1*10+i
for i in arr2:
num2 = num2*10+i
print (num1,num2)
#相加
num_total = num1+num2
print (num_total)
#从低位到高位写入链表,初始化链表的根节点为0,如果相加的和为0,直接返回
l_res = ListNode(0)
cursor = l_res
if num_total == 0: return l_res
while num_total:
temp = num_total%10
print (temp)
cursor.next = ListNode(temp)
cursor = cursor.next
num_total = int(num_total/10)
#print (num_total)
return l_res.next if __name__=='__main__':
#创建l1和l2两个链表,注意,排序好的就需要arr1和arr2中数字从小到大
arr1 = [0,8,6,5,6,8,3,5,7]
arr2 = [6,7,8,0,8,5,8,9,7]
l1 = ListNode(arr1[0])
p1 = l1
l2 = ListNode(arr2[0])
p2 = l2
for i in arr1[1:]:
p1.next = ListNode(i)
p1 = p1.next
for i in arr2[1:]:
p2.next = ListNode(i)
p2 = p2.next
s=Solution()
#两个链表相加
q=s.addTwoNumbers(l1,l2)

一些打印的输出:

753865680 798580876
1552446556
6
5
5
6
4
4
2
5
5
1

2. Add Two Numbers——Python的更多相关文章

  1. Leetcode2:Add Two Numbers@Python

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. Leetcode 解题 Add Two Numbers Python

    原题: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  3. leetcode add two numbers python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  6. LeetCode 2. add two numbers && 单链表

    add two numbers 看题一脸懵逼,看中文都很懵逼,链表怎么实现的,点了debug才看到一些代码 改一下,使本地可以跑起来 # Definition for singly-linked li ...

  7. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  8. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  9. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. 开启A20线(部分译)

    开启A20线 在查看或编写操作系统内核时一定会遇到A20线这个问题.本人对此一直都是似懂非懂的,查了些资料,决定弄明白于是有了这篇文章.其中前一部分是翻译一篇外国博文,但光有这篇文章依旧不能清楚地说明 ...

  2. c# ip内外网判断

    /// <summary> /// 判断IP地址是否为内网IP地址 /// </summary> /// <param name="ipAddress" ...

  3. vue组件

    分享出来让思路更成熟. 首先组件是 Vue.js 最强大的功能之一. 可以减少很多的工作量,提高工作效率. 编写一个可复用性的组件,虽然官网上也有.... 编写可复用性的vue组件 具备一下的几个要求 ...

  4. ThinkPHP的URL访问

    url访问 http://www.kancloud.cn/manual/thinkphp5/118012 ThinkPHP5.0在没有启用路由的情况下典型的URL访问规则是: http://serve ...

  5. RabbitMQ修改端口号和心跳时间

    定读者正在Windows系列的操纵体系中装置好了Rabbit MQ后,请找到装置途径下的etc文件夹下,将rabbitmq.config.example备份一份,并点窜为config范例的文件,即为] ...

  6. Nacl开发

    环境搭建:http://www.bojinxiaozhu.com/2014/0221/80.html http://blog.csdn.net/xoyojank/article/details/814 ...

  7. QuartusII Design partion and logic lock

    Design partion Design partion常用于“增益变量(QIC)”,通过Design Partition对子模块进行“逻辑分区”,在Design Partition Window中 ...

  8. iOS小Tip之查看FPS

    可能大家有的时候会想要查看app在运行时的帧率能否达到60帧,如果达不到的话,你可能会想着去优化动画或者其它任何会影响显示性能的问题. 但是,你首先要观察到你的FPS,对吧? 我告诉大家一个简单的方法 ...

  9. iOS 用户的隐私数据-privacy-sensitive data

    1  Xcode 报错:This app has crashed because it attempted to access privacy-sensitive data without a usa ...

  10. oracle学习笔记(二)

    1. Oracle字符串操作 1.1. 字符串类型 1.1.1. CHAR和VARCHAR2类型 CHAR和VARCHAR2类型都是用来表示字符串数据类型,用来在表中存放字符串信息, 比如姓名.职业. ...