You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

20180223

  1. class Solution {
  2. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  3. ListNode fakehead = new ListNode(0);
  4. ListNode prev = fakehead;
  5. int carry = 0;
  6. for(ListNode p1=l1, p2 = l2;
  7. p1!=null || p2!=null;
  8. p1=(p1==null?null:p1.next),p2=(p2==null?null:p2.next)
  9. ){
  10. int p1val = p1==null?0:p1.val;
  11. int p2val = p2==null?0:p2.val;
  12. int val = p1val+p2val+carry;
  13. carry = val/10;
  14. val = val%10;
  15. ListNode temp = new ListNode(val);
  16. prev.next = temp;
  17. prev = prev.next;
  18. }
  19. if(carry>0)
  20. prev.next = new ListNode(carry);
  21. return fakehead.next;
  22. }
  23. }
  1. class Solution {
  2. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  3. // ListNode cur =new ListNode(0);
  4. ListNode prev= new ListNode(0);
  5. ListNode head = prev;
  6. int pval;
  7. int jinwei=0;
  8. while(l1!=null ||l2!=null || jinwei!= 0 ){
  9. pval = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + jinwei;
  10. if(pval>9) { jinwei =1;pval=pval-10; }
  11. else jinwei=0;
  12. ListNode cur = new ListNode(pval);
  13. prev.next =cur;
  14. prev = cur;
  15. l1 = (l1 == null) ? l1 : l1.next;
  16. l2 = (l2 == null) ? l2 : l2.next;
  17.  
  18. }
  19. return head.next;
  20.  
  21. }
  22.  
  23. }
 

2. Add Two Numbers(2个链表相加)的更多相关文章

  1. [LeetCode] Add Two Numbers 两个数字相加

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

  2. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  3. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  4. [LeetCode] 2. Add Two Numbers 两个数字相加

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

  5. [LintCode] Add Two Numbers 两个数字相加

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  6. 2.Add Two Numbers-两个单链表相加

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

  7. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  8. 2. Add Two Numbers[M]两数相加

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

  9. 445 Add Two Numbers II 两数相加 II

    给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...

随机推荐

  1. 1-0 superset的安装和配置

    Superset安装及教程官网(http://airbnb.io/superset/installation.html)讲解的已经够详细的了,本篇以官网教程为蓝本进行说明. 入门 Superset目前 ...

  2. c++11——模板的细节改进

    c++11改进了编译器的解析规则,尽可能的将多个右尖括号(>)解析为模板参数结束符,方便编写模板相关的代码. 1. 模板的右尖括号 之前的c++标准中,模板套模板中右尖括号不能连在一块,否则会和 ...

  3. Java中DESKeySpec类

    此类位于 javax.crypto.spec 包下.声明如下: public class DESKeySpec extends Object implements KeySpec 此类指定一个 DES ...

  4. rpm方式安装 gitlab centos7

    一.使用RPM安装 - 推荐 官方推荐的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/ Tips 1 : Centos 7使用el7 ...

  5. Array.prototype.forEach数组遍历

    forEach是Array新方法中最基本的一个,就是遍历,循环.先看以前是怎么遍历数组的 常用遍历 var arr = [1,2,3,4,5]; for(var i = 0; i < arr.l ...

  6. js中的颜色对应的常量代码code

    颜色的对照表 颜色 英文代码 形像颜色 HEX格式 RGB格式   LightPink 浅粉红 #FFB6C1 255,182,193   Pink 粉红 #FFC0CB 255,192,203   ...

  7. 170717、springboot编程之mybatis数据库开发和aop拦截

    一.springboot整合mybaits (1)新建maven project; 新建一个maven project,取名为:spring-boot-mybatis (2)在pom.xml文件中引入 ...

  8. PL/SQL编程基础(五):异常处理(EXCEPTION)

    异常处理 异常产生所带来的问题: 使用EXCEPTION程序块进行异常处理: 实现用户自定义异常. 使用异常可以保证在程序中出现运行时异常时程序可以正常的执行完毕: 用户可以使用自定义异常进行操作. ...

  9. Ora-1157 ora-1110错误解决案例一枚

    1.数据库打开报错如下: SQL> alter database open; alter database open * ERROR at line 1: ORA-01157: cannot i ...

  10. thinkphp中 volist循环的 mod取值的问题

    <ul> <volist name="data" id="arr" key="k" mod="2"&g ...