[算法题] Add Two Numbers
题目内容
题目来源:LeetCode
You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题目思路
这个题目是倒序存储了两个数字,然后模拟进行加法运算。
本题中需要学习到的知识有:
1. 分别使用/和%来代表进位符号flag和当前位的值value
2. 成立哑结点(头结点),这样可以方便很多。
Python代码
# 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
if not l1:
return l2
if not l2:
return l1
dummy=ListNode(-1)
p=dummy
flag=0
while l1 and l2:
value=(l1.val+l2.val+flag)%10
p.next=ListNode(value)
p=p.next
flag=(l1.val+l2.val+flag)/10
l1=l1.next
l2=l2.next
while l1:
value=(l1.val+flag)%10
p.next=ListNode(value)
p=p.next
flag=(l1.val+flag)/10
l1=l1.next
while l2:
value=(l2.val+flag)%10
p.next=ListNode(value)
p=p.next
flag=(l2.val+flag)/10
l2=l2.next
if flag>0:
p.next=ListNode(flag)
return dummy.next
[算法题] Add Two Numbers的更多相关文章
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- LeetCode算法题-Self Dividing Numbers(Java实现)
这是悦乐书的第305次更新,第324篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第173题(顺位题号是728).自分割数是一个可被其包含的每个数字整除的数字.例如,12 ...
- LeetCode算法题-Find All Numbers Disappeared in an Array(Java实现)
这是悦乐书的第232次更新,第245篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第99题(顺位题号是448).给定一个整数数组,其中1≤a[i]≤n(n =数组的大小) ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode算法题-Add Strings(Java实现)
这是悦乐书的第223次更新,第236篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第90题(顺位题号是415).给定两个非负整数num1和num2表示为字符串,返回num ...
- LeetCode算法题-Add Digits(Java实现-3种解法)
这是悦乐书的第199次更新,第207篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第63题(顺位题号是258).给定非负整数num,重复添加其所有数字,直到结果只有一位数 ...
- LeetCode算法题-Add Binary(Java实现)
这是悦乐书的第157次更新,第159篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第16题(顺位题号是67).给定两个二进制字符串,返回它们的总和(也是二进制字符串).输 ...
- leetcode第四题--Add Two Numbers
Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...
随机推荐
- 【Android Developers Training】 88. 使用备份API
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- workday1
前天是实习的第一天,现在补下感想 总的来说还是不错的,师兄很nice,师妹很羞涩,我很尴尬,我的交际能力还是有待提高(主要是普通话不标准~~~~(>_<)~~~~) 早上由华工C12穿梭到 ...
- vue实现对表格数据的增删改查
在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...
- JS将文件以form表单一样提交到后台
这是很简单.. HTML <div> <input type="file" id="myfile"> <input type=&q ...
- voa 2015 / 4 / 15
illustrated - v. to explain or decorate a story, book, etc., with pictures pediatrician – n. a docto ...
- docker managed volume - 每天5分钟玩转 Docker 容器技术(40)
docker managed volume 与 bind mount 在使用上的最大区别是不需要指定 mount 源,指明 mount point 就行了.还是以 httpd 容器为例: 我们通过 - ...
- iOS多线程开发之NSThread
一.NSThread基本概念 NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程,需要手动管理线程的生命周期,处理线程同 ...
- sublime使用总结
上周忙呀忙~ 周一到五在忙项目,周六日搬家 在帝都平均一年就要换一次房子,从开始找房子到成功住进去前前后后大约花了半个多月的时间 什么时候就有自己的小窝了-- 之前开发一直用的都是W ...
- CJOJ 1644 编辑距离 / Luogu 2758 编辑距离(动态规划)
CJOJ 1644 编辑距离 / Luogu 2758 编辑距离(动态规划) Description 字符串是数据结构和计算机语言里很重要的数据类型,在计算机语言中,对于字符串我们有很多的操作定义,因 ...
- redhat设置开机自动连接网络
一.设置开机自动连接网络1.用root账号登录2.打开etcsysconfignetwork-scrpts目录3.vi ifcfg-eth04.将ONBOOT改为yes 二.没有图形界面如何连接网络1 ...