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 ...
随机推荐
- Java笔记--网络编程
1.IP地址:InetAddress类 --唯一的标识Internet上的计算机 --本地回环地址(hostAddress)127.0.0.1 主机名(hostName):localhost //根据 ...
- Day8 - B - Non-Secret Cypher CodeForces - 190D
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their nativ ...
- mybatis-generator-plugin
1.背景 这篇文章刚开始想着哪个分类呢?mybatis.idea或是maven呢,最后还是选择了mybatis.最初使用这个逆向工具是在eclipse上,使用的是eclispe上mbg插件执行配置ge ...
- 036、Java中三目运算符的使用
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-pause
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- C#常用类库简介(二)
原文出处:http://blog.csdn.net/weiwenhp/article/details/8140503 C#常用类库简介(一)的地址 System与mscorlib这两个dll中的类库是 ...
- GNS3 模拟icmp禁止不可达
R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...
- python csv
Python 读取csv的某行 Python 读取csv的某列 Python写了一个可以提取csv任一列的代码,欢迎使用.Github链接 两个list写入csv文件 column1,column2 ...
- spring boot项目mybatis配置注解+配置文件
maven依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-ja ...
- Pyinstaller的安装及简单使用
(1)安装: 用传统的pip install pyinstaller出错,在https://pypi.org/project/PyInstaller/#files上下载PyInstaller-3.4. ...