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 ...
随机推荐
- .Net 中读写Oracle数据库常用两种方式
.net中连接Oracle 的两种方式:OracleClient,OleDb转载 2015年04月24日 00:00:24 10820.Net 中读写Oracle数据库常用两种方式:OracleCli ...
- git配置正确且权限已开但是pull或push提示无权限
因为之前提示输入用户名和密码时输入错误,之后就一直权限认证失败.这种情况下在git bash中输入: git config --system --unset credential.helper 就会重 ...
- C++类有继承时,析构函数必须为虚函数
C++类有继承时,析构函数必须为虚函数.如果不是虚函数,则使用时可能存在内在泄漏的问题. 假设我们有这样一种继承关系: 如果我们以这种方式创建对象: SubClass* pObj = new SubC ...
- 部分用户访问Polycom视频会议时故障
1.现象 Polycom视频会议服务器部署在防火墙下,通过Paloalto防火墙的一对一映射到公网. 部分同事使用职场网络或者4G通过公网访问时,出现超时问题. 2.分析: Polycom设备并没有做 ...
- 【转载】Windows上那些值得推荐的良心软件-整理 easybcd 引导工具 easyuefi 引导工具
您查询的关键词是:清理dism知乎 以下是该网页在北京时间 2019年03月17日 21:56:16 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. 百度和网页 htt ...
- PHP开发——环境安装
PHP环境 Apache IDE的选择
- boost的下载和安装(windows版)
1 简介 boost是一个准C++标准库,相当于STL的延续和扩充,它的设计理念和STL比较接近,都是利用泛型让复用达到最大化. boost主要包含以下几个大类: 字符串及文本处理.容器.迭代器(it ...
- 阿里云ECS装LAMP环境
学生计划9.9买个一个ECS,要做PHP开发,所以搭建一个Lamp的环境 1.使用镜像,附件 sh-1.5.5附于文后. 2.一键安装 2.1 输入命令:chmod –R 777 sh-1.5.5 c ...
- redis CentOS6.5安装及集群部署
.下载redis source包 链接:https://pan.baidu.com/s/122ZCjNvjl9Jx6M2YsLrncw 密码:92ze 2.解压 tar -xzf redis-3.2. ...
- trinitycore 魔兽服务器源码分析(三) 多线程相关
先看LockedQueue.h template <class T, typename StorageType = std::deque<T> >class LockedQue ...