给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。 你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807

代码实现:

 class ListNode:
def __init__(self, x):
self.val = x
self.next = None def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
val_sum = l1.val + l2.val
list_node = ListNode(val_sum % 10)
a = val_sum // 10
node = list_node
while True:
try:
l1 = l1.next
except:
pass
try:
l2 = l2.next
except:
pass
if not l1 and not l2:
break
elif not l1:
l1_val = 0
l2_val = l2.val
elif not l2:
l2_val = 0
l1_val = l1.val
else:
l1_val = l1.val
l2_val = l2.val
val_sum = l1_val + l2_val + a
temp_node = ListNode(val_sum % 10)
node.next = temp_node
node = temp_node
a = val_sum // 10
if a != 0:
node.next = ListNode(a)
return list_node

注:这是在网上做的练习题,记录一下,有需要的时候方便自己查看。

python求两个链表组成的数字的和的更多相关文章

  1. [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交集

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  3. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  4. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. python求两个列表的并集.交集.差集

    求两个列表的差集 >>> a = [1,2,3] >>> b=[1,2] >>> ################################ ...

  6. [LeetCode]求两个链表的焦点--Intersection of Two Linked Lists

    标题题目地址 1.解题意 求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的. 落实到java层面,就是交点处的对象是同一个对象即可. ps:我最开始没有读懂题目,然后 ...

  7. python 求两个数的最大公约数

    给定两个整数a,b,求他们的最大公约数 def gcd(a,b): if a<b: a,b=b,a while(a%b != 0): c = a%b a=b b=c return b a,b = ...

  8. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  9. python 求两个时间差

    def timeInterval(self): today = datetime.date.today() print today modifiedTime = os.stat(filename).s ...

随机推荐

  1. POJ2127 Greatest Common Increasing Subsequence

    POJ2127 给定两个 整数序列,求LCIS(最长公共上升子序列) dp[i][j]表示A的A[1.....i]与B[1.....j]的以B[j]为结尾的LCIS. 转移方程很简单 当A[i]!=B ...

  2. java静态方法和实例化方法的区别(copy)

    [资料来源] http://blog.csdn.net/biaobiaoqi/article/details/6732117 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法 ...

  3. bzoj 1578: [Usaco2009 Feb]Stock Market 股票市场【背包】

    参考:https://blog.csdn.net/mars_ch/article/details/53011234 我背包真是好不熟练啊-- 第一天买了第三天卖相当于第一天买了第二天卖第二天再买第三天 ...

  4. bzoj 4297: [PA2015]Rozstaw szyn【瞎搞】

    从叶子往上先拓扑一下,建立虚拟root,从root开始dfs.注意到每个点的最优取值一定是一个区间(中位数区间),从儿子区间推出父亲区间即可 #include<iostream> #inc ...

  5. jQuery多项选择器

    jQuery多项选择器模式: $("selector1,selector2,selectorN"); 将每一个选择器匹配到的元素合并后一起返回,可以指定任意多个选择器,并将匹配到的 ...

  6. js 事件循环机制 EventLoop

    js 的非阻塞I/O  就是由事件循环机制实现的 众所周知  js是单线程的 也就是上一个任务完成后才能开始新的任务 那js碰到ajxa和定时器.promise这些异步任务怎么办那?这时候就出现了事件 ...

  7. C++面向对象程序设计_Part2

    目录 Composition(复合) 内存视角下的composition(复合) composition(复合)关系下的构造与析构 Delegation (委託) -- Composition by ...

  8. java自学-方法

    上节介绍了流程控制语句,一个复杂的业务逻辑会由很多java代码组成,包含许多功能.比如说购物业务,就包含选商品.下单.支付等功能,如果这些功能的代码写到一起,就会显得很臃肿,可读性非常不好.java提 ...

  9. JavaScript编程艺术-第10章-10.2-实用的动画

    10.2-实用的动画 ***代码亲测可用*** HTML: <!DOCTYPE HTML> <html> <head> <meta charset=" ...

  10. [C++ STL] 迭代器(iterator)详解

    背景:指针可以用来遍历存储空间连续的数据结构,但是对于存储空间非连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历.因此,我们引入迭代器概念.   一.迭代器(iterator)介绍 ...