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的更多相关文章

  1. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  2. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  3. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

  6. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  8. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

    题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  9. leetcode刷题: 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. quick pdf library控件

    quick pdf library库只能在windows环境下运行 1.下载quick pdf library 2.注册控件 进入cmd  regsvr32 "C:\Program File ...

  2. LINQ之let关键字

    let子句用于在LINQ表达式中存储子表达式的计算结果.let子句创建一个范围变量来存储结果,变量被创建后,不能修改或把其他表达式的结果重新赋值给它.此范围变量可以再后续的LINQ子句中使用. 实例1 ...

  3. viewstate?

    用于存放数据,可永久保存,存放在页面的隐藏控件里      支持string,integer,array,boolean,ArrayList,hashtable类型,使用viewstate会增加页面h ...

  4. centos 7 vim 保存文件 root 权限 及 使用

    vim打开文件时,没有用root账户,保存时才发现需要root权限.办法是在vim执行一下命令: :w !sudo tee % 未完待续

  5. springmvc mybatis shiro构建cms系统

    开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...

  6. delphi三层结构常出现的问题和解决方案

    以下问题出现原因有可能多个,暂时将我遇见的记录下来,以后有新的在陆续更新上去,有网友愿意的话也可以共同测试一下. 一,无法更新定位行.一些值可能已在最后一次读取已更改. 错误出现前提: 1, 录数据时 ...

  7. AX_DbSynchronize

    static void KTL_DBSynchronize(Args _args)  {      Dictionary dict;      int idx, lastIdx, totalTable ...

  8. 别人的Linux私房菜(12)正则表达式与文件格式化处理

    vi gerp awk sed支持正则表达式   cp ls不支持,只能使用bash本身的通配符 正则表达式分为基础正则表达式和拓展正则表达式 使用正则表达式注意语系的影响 http://cn.lin ...

  9. 2019.02.21 bzoj2739: 最远点(决策单调性+分治)

    传送门 题意简述:给一个N个点的凸多边形,求离每一个点最远的点. 思路:先根据初中数学知识证明决策是满足单调性的,然后上分治优化即可. 才不是因为博主懒得写二分+栈优化呢 代码: #include&l ...

  10. Log4J日志整合及配置详解

    Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使用这三个组件可以轻松 ...