LeetCode--No.002 Add Two Numbers
Add Two Numbers
- Total Accepted: 160702
- Total Submissions: 664770
- Difficulty: Medium
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
本题的思路其实很简单,两个链表的结构都是从低位到高位的顺序,税后要求返回的链表也是这样的结构。所以我们只需要依次循环两个链表,将对应位的数相加,并判断是否有进位,有进位则将进位累加到下一位即可。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Num2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null){
return l2 ;
}
if(l2 == null){
return l1 ;
}
int nextBit = (l1.val + l2.val)/10 ;
int curBit = (l1.val + l2.val)%10 ;
ListNode head = new ListNode(curBit) ;
ListNode temp = head ;
l1 = l1.next ;
l2 = l2.next ;
//当l1、l2对应位均存在时,进行计算
while(l1 != null && l2 != null){
curBit = (l1.val + l2.val + nextBit) % 10 ;
nextBit = (l1.val + l2.val + nextBit)/10 ;
ListNode node = new ListNode(curBit) ;
temp.next = node ;
temp = temp.next ;
l1 = l1.next ;
l2 = l2.next ;
}
//判断l1是否结束,没有结束继续
while(l1 != null){
curBit = (l1.val + nextBit) % 10 ;
nextBit = (l1.val + nextBit)/10 ;
ListNode node = new ListNode(curBit) ;
temp.next = node ;
temp = temp.next ;
l1 = l1.next ;
}
//判断l2是否结束,没有结束继续
while(l2 != null){
curBit = (l2.val + nextBit) % 10 ;
nextBit = (l2.val + nextBit)/10 ;
ListNode node = new ListNode(curBit) ;
temp.next = node ;
temp = temp.next ;
l2 = l2.next ;
}
//判断最后的进位位是否为0 ,不为0则需要保存下一位
if(nextBit != 0){
ListNode node = new ListNode(nextBit) ;
temp.next = node ;
}
return head ;
}
}
LeetCode--No.002 Add Two Numbers的更多相关文章
- 【LeetCode】002 Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
- 《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刷题: 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- 46 【golang项目】完成了一个小小的播放器功能
项目地址:https://github.com/helww/mylab/tree/master/go/player 这个项目中用到了readme说明文件是一个markdown文件. 基础的控制语法,网 ...
- thinkphp 查表返回的数组,js解析有UNICode编码,解决办法
public function getDeviceMsg(){ $allDevicesMsg = M("newdevicesstatus")->getField(" ...
- Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题(转)
原文地址:https://wzfou.com/php-fpm/ 最近发现博客的内存老是隔三差五地被“吃掉”了,登录到后台后偶尔会出卡顿的情况,一开始怀疑是Swap不够导致的,于是给VPS主机增加了几个 ...
- 20175234 2018-2019-2 《Java程序设计》第四周学习总结
20175234 2018-2019-2 <Java程序设计>第四周学习总结 教材学习内容总结 教材学习了子类,其重点是方法重写.对象的上转型对象和多态,强调了面向抽象编程的思想. 学习I ...
- Eigen矩阵基本运算
1 矩阵基本运算简介 Eigen重载了+,-,*运算符.同时提供了一些方法如dot(),cross()等.对于矩阵类的运算符重载只支持线性运算,比如matrix1*matrix2是矩阵相乘,当然必须要 ...
- HDU 3667.Transportation 最小费用流
Transportation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Spring 添加属性集中常见方法
//创建容器,索要对象, package cn.lijun.Test; import org.junit.Test;import org.springframework.context.Applica ...
- You are using pip version 9.0.1, however version 9.0.3 is available.
1,pip不能用了,提示:You are using pip version 8.1.1, however version 9.0.1 is available.网上搜索了一箩筐的安装程序,各种安装, ...
- ios下面的按钮和inout框
在ios系统中,按钮和输入框,会默认给你加一个圆角和阴影,可以用css去掉这个自带的属性 input[type=button], input[type=submit], input[type=file ...
- LOJ-10100(割点个数)
题目链接:传送门 思路: 就是求割点的个数,直接Tarjan算法就行. 注意输入格式(判断比较水). #include<iostream> #include<cstdio> # ...