leetcode445 Add Two Numbers II
"""
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
"""
"""
与leetcode2 类似。
这两题都是自己做出来的
"""
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def addTwoNumbers(self, l1, l2):
num1, num2 = 0, 0
if l1 and not l2:
return l1
if l2 and not l1:
return l1
p1, p2 = l1, l2 #将链表l1 和 l2 转成数字num1 和num2 求和得num
while p1:
num1 = num1 * 10 + p1.val
p1 = p1.next
while p2:
num2 = num2 * 10 + p2.val
p2 = p2.next
num = num1 + num2
res = ListNode(0) #头结点
end = ListNode(num % 10) #尾结点
res.next = end
end.next = None
num = num // 10
while num: #从链表尾部插入当前结点cur
cur = ListNode(num % 10) #当前结点
res.next = cur
cur.next = end
end = cur
num = num // 10
return res.next
leetcode445 Add Two Numbers II的更多相关文章
- LeetCode 445. 两数相加 II(Add Two Numbers II)
445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 445. Add Two Numbers II - LeetCode
Question 445. Add Two Numbers II Solution 题目大意:两个列表相加 思路:构造两个栈,两个列表的数依次入栈,再出栈的时候计算其和作为返回链表的一个节点 Java ...
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- LeetCode 445 Add Two Numbers II
445-Add Two Numbers II You are given two linked lists representing two non-negative numbers. The mos ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- [Swift]LeetCode445. 两数相加 II | Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- Follow somebody
networkersdiary A personnel blog with Network Engineering articles https://networkersdiary.com/cisco ...
- formValidation单个输入框值改变时校验
$("#tv_form").data("formValidation").updateStatus("pay.vcAmount", &qu ...
- UVA10723 电子人的基因
UVA10723 电子人的基因 题目比较难找附上链接:https://vjudge.net/problem/UVA-10723 题目描述: 给你两个字符串,你需要找出一个最短的字符串,使得两个给定字符 ...
- Fiddler一次性发多个请求
Fiddler一次发送多个请求 选中某个请求: 选中 : Raw, 将request数据拷出: 包含请求header和request body 替换request header里面的ASP.NET_S ...
- lc 0219
目录 ✅ 463. 岛屿的周长 描述 解答 cpp py ✅ 1122. 数组的相对排序 描述 解答 cpp py ✅ 876. 链表的中间结点 描述 解答 cpp ✅ 1160. 拼写单词 描述 解 ...
- 重装VisualSVN Server报错
由于eclipse无法连接SVN服务器,尝试着重装SVN,安装到一半时,弹出如下图所示错误: 打开提示窗口输入services.msc,进入服务界面: 发现VisualSVN Server服务无法启动 ...
- Java入门笔记 03-面向对象(上)
介绍:Java是面向对象的程序设计语言,类是面向对象的重要内容,可以把类当成是一种自定义类型,可以使用类来定义变量,这种类型的变量统称为引用变量.也就是说,所有类都是引用类型.Java也支持面向对象的 ...
- 设计模式课程 设计模式精讲 5-2 工厂方法coding
1 课堂讲义 1.1 产品等级和产品簇 2 代码演练 2.1 工厂方法代码演练 1 课堂讲义 1.1 产品等级和产品簇 工厂方法是为了解决同一产品等级的业务抽象问题 抽象工厂方法是为了解决同一产品簇的 ...
- NSDateFormatter使用注意事项
NSDateFormatter是用来连接NSDate和NSString之间的桥梁 它的使用方式,不(自)做(行)说(百)明(度) 要说的注意事项就是,NSString转NSDate时,NSDateFo ...
- PAT A1131 Subway Map
dfs,选择最优路径并输出~ 这道题难度非常炸裂,要求完完整整自己推一遍,DFS才算过关!思路:一遍dfs,过程中要维护两个变量,minCnt 中途停靠最少的站.minTransfer需要换成的最少次 ...