Q2:Add Two Numbers
2. Add Two Numbers
官方的链接:2. Add Two Numbers
Description :
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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
问题描述
给定2个非空的代表非负整数的链表,数字是倒过来存储的,用链表表示求和。可以假设没有前导数字0。
思路
使用迭代解法,注意最后的进位。
public class Q2_AddTwoNumbers {
/**
* 迭代解法
*
* @param ListNode
* l1
* @param ListNode
* l2
* @return
*/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (null == l1 && null == l2) {
return new ListNode(0);
}
// 保存链表头,便于创建结点和最后返回结果
ListNode headNode = new ListNode(0);
ListNode sumNode = headNode;
// 和以及进位
int sum = 0;
int carry = 0;
while (null != l1 && null != l2) {
sum = l1.val + l2.val + carry;
sumNode.next = new ListNode(sum % 10);
carry = sum / 10;
sumNode = sumNode.next;
l1 = l1.next;
l2 = l2.next;
}
while (null != l1) {
sum = l1.val + carry;
sumNode.next = new ListNode(sum % 10);
carry = sum / 10;
sumNode = sumNode.next;
l1 = l1.next;
}
while (null != l2) {
sum = l2.val + carry;
sumNode.next = new ListNode(sum % 10);
carry = sum / 10;
sumNode = sumNode.next;
l2 = l2.next;
}
if (carry > 0) {
sumNode.next = new ListNode(carry);
sumNode = sumNode.next;
}
return headNode.next;
}
}
Q2:Add Two Numbers的更多相关文章
- LeetCode-2: Add Two Numbers
[Problem:2-Add Two Numbers] You are given two non-empty linked lists representing two non-negative i ...
- LeetCode4:Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- No.002:Add Two Numbers
问题: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- leetcode:Add Two Numbers
题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
- LeetCode第[2]题(Java):Add Two Numbers (链表相加)——Medium
题目难度:Medium 题目: You are given two non-empty linked lists representing two non-negative integers. The ...
- LeetCode OJ:Add Two Numbers (相加链表之数)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 面试题:Add Two Numbers(模拟单链表)
题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- LeetCode第二题:Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- pytesseract 识别率低提升方法
pytesseract 识别率低提升方法 一.跟换识别语言包 下载地址https://github.com/tesseract-ocr/tessdata 二.修改图片的灰度 from PIL impo ...
- JS: 随机点名程序与万年历
随机点名程序 document.write(Math.random()); var stu = ["张三", "王五", "张二", &qu ...
- mysql 分组查询入门
- C# enable为false时不变颜色
[System.Runtime.InteropServices.DllImport("user32.dll ")] public static ex ...
- Google的搜索API的Delphi封装
这个东西实现了已经有一段时间了,那个时候谷歌还没有退出中国内地呢!而现在呢,谷歌都退了有一些日子了!紧以此纪念一番! 话说谷歌API,我相信很多人应该都知道!不晓得在实际应用中,用的人多不多(我说的不 ...
- l5213. 玩筹码
这道题本应该很简单的但是我把他复杂化了,所以没有在第一时间里A出来.我们来看看题目 看上去是不是很复杂,思路是有,但是,很难实现.我最开始的时候是认为有三种情况,左边筹码最多,右边筹码最多,中间筹码最 ...
- MAC设置允许任何来源
在升级了macOS Sierra (10.12)版本后在“安全性与隐私”中不再有“任何来源”选项 接下来,我们就打开终端,然后输入以下命令: sudo spctl --master-disable 输 ...
- 096-PHP循环使用next取数组元素
<?php function return_item($arr,$num=0){ //定义函数 for($i=0;$i<$num;$i++){ //循环向前移动数组指针 next($arr ...
- HDU 3397 线段树 双懒惰标记
这个是去年遗留历史问题,之前思路混乱,搞了好多发都是WA,就没做了 自从上次做了大白书上那个双重懒惰标记的题目,做这个就思路很清晰了 跟上次大白上那个差不多,这个也是有一个sets标记,代表这个区间全 ...
- sourcetree安装以及跳过sourcetree注册登录 - git仓库管理工具桌面版
腾讯软件下载:https://pc.qq.com/detail/17/detail_23237.html 官网下载:https://www.sourcetreeapp.com/ 下载完直接安装 ...