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

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

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

  2. LeetCode--No.002 Add Two Numbers

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

  3. leetcode刷题: 002 Add Two Numbers

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

  4. 【JAVA、C++】LeetCode 002 Add Two Numbers

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

  5. 【LeetCode】002 Add Two Numbers

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

  6. 002 Add Two Numbers 链表上的两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  7. [Leetcode] 002. Add Two Numbers

    https://leetcode.com/problems/add-two-numbers/ public class Solution { public ListNode addTwoNumbers ...

  8. 002. Add Two Numbers

    题目链接:https://leetcode.com/problems/add-two-numbers/description/ Example: Input: (2 -> 4 -> 3) ...

  9. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

随机推荐

  1. (C/C++ interview) Static 详解

    C Static http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program Static could ...

  2. Report_SRW在RDF中初始化的重要性(案例)

    2015-02-01 Created By BaoXinjian 一.摘要 在开发oracle report(report 6i)的时候,常常会用到fnd_global或fnd_profile来获取当 ...

  3. group by 获取总记录数

    sql中有group buy 后如何获取总记录的条数,来生成分页 当然一般情况下我是不推荐这样的分页,如果你真的需要应该是你表结构设计有问题 1.适用于所有情况 $db = new PDO(DSN.. ...

  4. linux下shell脚本学习

    在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.您可以通过使用shell使大量的任务自动化,shel ...

  5. java中通过位运算实现多个状态的判断

    通过 <<  |  & ~ 位运算,实现同时拥有多个状态 通过 << 定义数据的状态 public interface LogConstants { /** * 消耗标 ...

  6. ios统计代码行数

    要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 列出每个文件的行数: find . -name "*.m" -or -name "*.h" ...

  7. Python标准库08 多线程与同步 (threading包)

    Python主要通过标准库中的threading包来实现多线程.在当今网络时代,每个服务器都会接收到大量的请求.服务器可以利用多线程的方式来处理这些请求,以提高对网络端口的读写效率.Python是一种 ...

  8. [AIR] 在 Adobe AIR 中为不同屏幕尺寸的多种设备提供支持

    转自:http://www.adobe.com/cn/devnet/air/articles/multiple-screen-sizes.html 无论是改编原本在浏览器 Flash Player 中 ...

  9. AngularJs创建服务

    在开发中我们总是需要向服务器请求同样的数据,那么我们如何来把他们提取出来进行封装一下呢,这就需要用到服务了. 需要用到关键字factory了. <!DOCTYPE html> <ht ...

  10. Codeforces 519E A and B and Lecture Rooms [倍增法LCA]

    题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分 ...