题目:

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. bzoj3052: [wc2013]糖果公园

    又是一代神题. uoj测速rank10,bzoj测速rank26(截止当前2016.5.30 12:58) 带修改的树上莫队. 修改很少,块的大小随便定都能A 然而我一开始把开3次根写成了pow(bl ...

  2. CSS中清除浮动的两种方式

    在CSS中,父元素中的子元素如果使用了float,会导致父元素塌陷,高度为0. 对于这种情况,常见的解决方式有两种. 一.增加新的div,应用clear:both属性 html: <div cl ...

  3. weiphp踩坑记录

    记录使用weiphp过程中遇到的问题及解决办法: 1.按照网上的教程创建插件怎么没有效果?replyText没有回复信息? 后台清除缓存即可!

  4. 【总结】富有表现力的JavaScript

    1.JavaScript的灵活性 JavaScript是目前最流行.应用最广泛的语言之一,它是一种极富表现力的语言,它具有C家族语言所罕见的特性.这种语言允许我们使用各种方式来完成同一个任务或者功能, ...

  5. NOIp2016 Day1&Day2 解题报告

    Day1 T1 toy 本题考查你会不会编程. //toy //by Cydiater //2016.11.19 #include <iostream> #include <cstd ...

  6. 遍历hashMap的两种方式

    第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...

  7. UNIX下的LD_PRELOAD环境变量

    UNIX下的LD_PRELOAD环境变量 也许这个话题并不新鲜,因为LD_PRELOAD所产生的问题由来已久.不过,在这里,我还是想讨论一下这个环境变量.因为这个环境变量所带来的安全问题非常严重,值得 ...

  8. quantum sets

    the principles of quantum mechanics by p.a.m.dirac.

  9. display:none显示和隐藏

    <html> <head> <title>显示和隐藏问题</title> <meta charset="utf-8"/> ...

  10. uboot 第三天学习

    make jCPU_NUM 以CPU_NUM数量同时进行编译CPU_NUM = 当前PC的处理器数量*处理器的核心数 从上电开始1.系统上电,执行固化在IROM中的代码,目的初始化基本的系统功能,已经 ...