LeetCode(2) - Add Two Numbers
一道比较基本的LinkedList的题目。题目要求是这样,现在有两个LinkedList,(2 -> 4 -> 3)和(5 -> 6 -> 4),然后从头开始,把每个node的value相加,最终输出的结果应该是:(7 ->0 ->8)。这里需要注意的边界条件有两个,一个是两个链表长度不一样的问题,另一个是进位的问题,注意了这两个问题以后,问题就比较好解决了。
具体的代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//对于链表来说,当需要处理头节点head的时候,本人比较喜欢用一个dump node, 通过
//dump node来指向头节点,从而省去很多判断是否为head的情况。最后返回dumpNode.next即可
ListNode dumpNode = new ListNode(0);
//用add来纪录是否进位。
int add = 0;
ListNode curr = dumpNode;
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + add;
//记录是否该进位,当sum > 9,则需要进位。
if (sum > 9) {
sum = sum % 10;
add = 1;
}
else add = 0;
curr.next = new ListNode(sum);
curr = curr.next;
l1 = l1.next;
l2 = l2.next;
} //l1长度>l2,则进入该循环,注意进位。
while (l1 != null) {
int sum = l1.val + add;
if (sum > 9) {
sum = 0;
add = 1;
}
else add = 0;
curr.next = new ListNode (sum);
curr = curr.next;
l1 = l1.next;
}
//l2 > l1的情况
while (l2 != null) {
int sum = l2.val + add;
if (sum > 9) {
sum = 0;
add = 1;
}
else add = 0;
curr.next = new ListNode(sum);
curr = curr.next;
l2 = l2.next;
}
//这个容易漏掉,来说明最后是否进位,如 [5]和[5]最终应输出[0->1]
if (add == 1) curr.next = new ListNode(1); return dumpNode.next;
}
}
LeetCode(2) - Add Two Numbers的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
- leetcode题解2. 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 linked lists representing two non-negative numbers. The digits are stored in ...
随机推荐
- 安装hma master出错 Error: Package: perl-Mail-Sender-0.8.13-2.el5.1.noarch
You are using the EPEL 5 version of the repo instead of 6, go into your /etc/yum.repos.d/epel.repo f ...
- js和 jquery对象
核心提示:jquery选择器得到的jquery对象和标准的 javascript中的document.getElementById()取得的dom对象是两种不同的对象类型,一般情况下,如S(’#id’ ...
- 下拉刷新控件(5)SwipeRefreshLayout官方教程(下)响应刷新事件
http://developer.android.com/training/swipe/respond-refresh-request.html This lesson shows you how t ...
- BZOJ 2743 采花(树状数组)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2743 题意:给出一个数列,每个询问查询[L,R]中至少出现两次的数字有多少种? 思路:(1 ...
- [HIHO1299]打折机票(线段树)
题目链接:http://hihocoder.com/problemset/problem/1299 线段树,按照t为下标去更新v,更新的时候要保留最大的那个. #include <algorit ...
- design pattern及其使用
什么是设计模式? design pattern是一个通用的,可以被重用的关于一个常见的问题的解决方案. 为什么要用设计模式? 引入设计模式的理论基础非常简单.我们每天都会碰到问题.我们可能碰到决定使用 ...
- Mybatis 插入与批量插入以及多参数批量删除
实体类: import java.io.Serializable; public class AttachmentTable implements Serializable { private sta ...
- bootstrapValidator对于隐藏域验证和程序赋值即时验证的问题
问题1: 如下代码: <input type="hidden" name="productId"/> $("#addForm") ...
- svn备份脚 本
一直用这套脚本备份,脚本主体虽不是原创,但是从网上得到后因为不能运行也进行了些修改,前两天看到有人问关于SVN备份的问题,今天又把脚本整理了一下,解决了不能循环备份多个配置库的问题.希望对大家有所帮助 ...
- 20160130.CCPP体系详解(0009天)
程序片段(01):hanoi.c+汉诺塔.c 内容概要:汉诺塔 ///hanoi.c #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> ...