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 ...
随机推荐
- AOJ 0525 - Osenbei
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=50785 翻译:http://blog.csdn.net/acm_10000h ...
- leetcode:Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- leetcode:Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- staging server, source congtrol, deply workflow using git
web项目开发中,有三个实践对于项目成功是非常重要的: 1. staging servers 2. Version control workflows 3. Tested, repeatable de ...
- bzoj4127
肯定是树链剖分+线段树,关键是怎么维护 绝对值和这个东西显然不能简单的合并标记 因为对于负数,加之后绝对值和是变小的 那我们考虑对负数和非负数数分别维护 下面的问题就是经过操作如果负数变成了正数怎么办 ...
- django - from django.db.models import F - class F
F() 的执行不经过 python解释器,不经过本机内存,是生成 SQL语句的执行. # Tintin filed a news story! reporter = Reporters.objects ...
- setTimeout/setInterval
//使用 setTimeout 时需注意,当该代码执行时,JS 会立即编译函数第一个参数“code” //所以该函数的第一个参数应该为:需要编译的代码.或者一个函数 //例1:setTimeout(& ...
- 【解题报告】[动态规划] RQNOJ - PID273 / 马棚问题
原题地址:http://www.rqnoj.cn/problem/273 题目大意:中文题不说了. 设从第i匹马到第j-1匹马放在一个马棚里得到的系数为f(i,j). 状态表示:dp[i][j]表示前 ...
- 函数式宏定义用do...while(0)的好处
函数式宏定义经常写成这样:<pre lang="c" escaped="true">#define device_init_wakeup(dev, ...
- Android View绘制13问13答
1.View的绘制流程分几步,从哪开始?哪个过程结束以后能看到view? 答:从ViewRoot的performTraversals开始,经过measure,layout,draw 三个流程.draw ...