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 ...
随机推荐
- [Android]如何导入已有的外部数据库
转自:http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html 我们平时见到的android数据库操作一般都是在程序开始时创建一 ...
- php绕过open_basedir设置
原理关于open_basedir open_basedir是php.ini中的一个配置选项 它可将用户访问文件的活动范围限制在指定的区域, 假设open_basedir=/home/ ...
- JAVA CookieUtil
package com.itheima.shop.utils; import java.io.UnsupportedEncodingException; import java.net.URLDeco ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:屏幕阅读器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Problem B: Bulbs
Problem B: Bulbs Greg has an m×n grid of Sweet Lightbulbs of Pure Coolness he would like to turn on. ...
- POJ 2287 田忌赛马
Tian Ji -- The Horse Racing Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 17699 Acc ...
- ubuntu 中加速pip指令下载插件的速度
在使用pip下载时很多时候下载速度特别慢,时不时就会发生timeout. 这是因为安装源与本机之间网络不畅导致,其实可以自己指定pip的下载来源,就像指定ubuntu更新源那样. 接下来谈谈步骤: 1 ...
- 51nod 1208 && POJ 2482:Stars in Your Window
1208 Stars in Your Window 题目来源: Poj 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 收藏 取消关注 整点上有N颗星星,每颗 ...
- Liunx用户运行模式
运行模式也可以称之为运行级别(Running Level). 在linux中存在一个进程:init (initialize,初始化),进程id是1. [he@localhost ~]$ ps -ef ...
- [LeetCode] 933. Number of Recent Calls 最近的调用次数
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...