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 ;
}
}
No.002 Add Two Numbers的更多相关文章
- LeetCode #002# Add Two Numbers(js描述)
索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...
- LeetCode--No.002 Add Two Numbers
Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...
- leetcode刷题: 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】002 Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- 002 Add Two Numbers 链表上的两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [Leetcode] 002. Add Two Numbers
https://leetcode.com/problems/add-two-numbers/ public class Solution { public ListNode addTwoNumbers ...
- 002. Add Two Numbers
题目链接:https://leetcode.com/problems/add-two-numbers/description/ Example: Input: (2 -> 4 -> 3) ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
随机推荐
- [platform]linux platform device/driver(二)--Platform Device和Platform_driver注册过程之详细代码
转自:http://www.cnblogs.com/haimeng2010/p/3582403.html 目录: 1.platform_device注册过程 2.platform_driver注册过程 ...
- 从官方下载 Bootstrap 版本 并写 第一个页面
从官方下载 Bootstrap 版本 页面内容参考自 http://www.cnblogs.com/sanjuantianshu/p/3935120.html bootstrap-3.2.0.zip ...
- 【MySQL】unique列插入重复值解决方案
当在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ON DUPLICATE KEY UPDATE子句跳过INSERT.中断操作或者更新旧 ...
- ADF_Advanced ADF系列2_Fusion应用的客制和个性化(Part2)
2015-02-17 Created By BaoXinjian
- 2016 Multi-University Training Contest 5 Two
本文转自:http://blog.csdn.net/queuelovestack/article/details/52096337 题意: 给你两个序列A和B 问两个序列有多少个子序列一样 例如{1, ...
- NeHe OpenGL教程 第十六课:雾
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- openmp并行计算
#include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...
- weinre 手机端页面调试
Weinre代表 Web Inspector Remote,是一种远程调试工具, 采用nodejs做为http服务器,用ajax通信 nodejs 官网 weinre官网 1.安装nodejs 会自 ...
- Linux重置mysql密码(转载)
From:http://hi.baidu.com/mcspring/item/6358ee27afe7e1c8a5275ab7 首先,必须拥有MySQL操作的所有权限: 其次,停止MySQL服务: / ...
- tar.gz和rpm安装文件(转载)
from:http://bbs.chinaunix.net/thread-2277750-1-1.html Linux软件的二进制分发是指事先已经编译好二进制形式的软件包的发布形式,其优点是安装使用容 ...