python求两个链表组成的数字的和
给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。 你可以假设除了数字 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求两个链表组成的数字的和的更多相关文章
- [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 ...
- [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 ...
- Python 求两个文本文件以行为单位的交集 并集 差集
Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...
- ✡ 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 ...
- python求两个列表的并集.交集.差集
求两个列表的差集 >>> a = [1,2,3] >>> b=[1,2] >>> ################################ ...
- [LeetCode]求两个链表的焦点--Intersection of Two Linked Lists
标题题目地址 1.解题意 求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的. 落实到java层面,就是交点处的对象是同一个对象即可. ps:我最开始没有读懂题目,然后 ...
- 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 = ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- python 求两个时间差
def timeInterval(self): today = datetime.date.today() print today modifiedTime = os.stat(filename).s ...
随机推荐
- Linux/Android——输入子系统input_event传递 (二)【转】
本文转载自:http://blog.csdn.net/jscese/article/details/42099381 在前文Linux/Android——usb触摸屏驱动 - usbtouchscre ...
- python cmd 启动python项目报错:no module named “xxx”
场景:使用pycharm编辑器启动pyhon项目时可以启动,但使用cmd启动时,会报:no module named “xxx”的错误,此时,有两种情况: 1.no module named “xxx ...
- HTML5移动Web开发
1. 响应式web设计 说到这个,移动开发面对的屏幕尺寸那叫一个丰富,其中安卓阵营就够让人头痛的.我们在PC端常用的两种布局方式就是固定布局和弹性布局,前者设置一个绝大多数电脑能正常显示的固定宽度居中 ...
- 引水入城 2010年NOIP全国联赛提高组(bfs+贪心)
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个遥远 ...
- 13、git
安装Git 网上有很多Git安装教程,如果需要图形界面,windows下建议使用TortoiseGit,linux建议使用Git GUI或者GITK.(windows下载exe安装包,linux可以使 ...
- 数据结构之单链表(C实现)
list.h #ifndef LIST_H #define LIST_H #include <iostream> #include <stdio.h> #include < ...
- JD笔试
题目表述: 给定n道题目,以及每道题目答对的概率,问小明能及格的概率. 样例: 40 50 50 50 50 0.31250 思路: 递归枚举对的题目个数,最后TLE之过40%: 知道正确解法是DP, ...
- mysql 更改字符集
Windows: 安装目录下新建my.ini文件,输入一下内容 [mysqld]#修改服务器端默认字符编码格式为utf8character-set-server = utf8 [client]#修改客 ...
- 【转】Java实现将文件或者文件夹压缩成zip
转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...
- 本地编译全志R系列的步骤7(Ubuntu 17.04非长期支持版本)
本地编译全志R系列的步骤7(Ubuntu 17.04非长期支持版本) 2017/6/29 13:49 0.获取全志R系列的Android源码包: 请通过渠道/代理商/方案公司获取全志R系列的Andro ...