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

 class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode fakehead = new ListNode(0);
ListNode prev = fakehead;
int carry = 0;
for(ListNode p1=l1, p2 = l2;
p1!=null || p2!=null;
p1=(p1==null?null:p1.next),p2=(p2==null?null:p2.next)
){
int p1val = p1==null?0:p1.val;
int p2val = p2==null?0:p2.val;
int val = p1val+p2val+carry;
carry = val/10;
val = val%10;
ListNode temp = new ListNode(val);
prev.next = temp;
prev = prev.next;
}
if(carry>0)
prev.next = new ListNode(carry);
return fakehead.next;
}
}
 class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// ListNode cur =new ListNode(0);
ListNode prev= new ListNode(0);
ListNode head = prev;
int pval;
int jinwei=0;
while(l1!=null ||l2!=null || jinwei!= 0 ){
pval = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + jinwei;
if(pval>9) { jinwei =1;pval=pval-10; }
else jinwei=0;
ListNode cur = new ListNode(pval);
prev.next =cur;
prev = cur;
l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next; }
return head.next; } }
 

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. 微信jssdk批量添加卡券接口(踩坑经验)

    1)首先是官方接口文档: 1.批量添加卡券接口:https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.0861973 ...

  2. Python 入门(一)定义字符串+raw字符串与多行字符串

    定义字符串 前面我们讲解了什么是字符串.字符串可以用''或者""括起来表示. 如果字符串本身包含'怎么办?比如我们要表示字符串 I'm OK ,这时,可以用" " ...

  3. phpstrom配置

  4. beginUpdates和endUpdates-实现UITableView的动画块

    我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...

  5. AndroidのListView之滑动列表项(点击事件和滑动事件共存)

    这里正好在项目有这么一个bt的需求,如下图ListView的item可以响应点击事件也可以响应item的左右滑动事件,两个事件可以相互独立互不影响. 听说iphone的list选项就有这样bt的功能, ...

  6. 什么是runtime?什么是webgl?

    一 什么是Runtime? Egret官方解释:https://www.egret.com/products/runtime.html 二.什么是WebGL渲染? egret官方解释:http://d ...

  7. Navicat 创建 Mysql 函数

    1.点击新建函数 2.写函数,保存为v1 3.调用 SELECT id,v1(id) from 表

  8. ORA-01153: an incompatible media recovery is active

    ORA-01153: an incompatible media recovery is active Cause: Attempted to start an incompatible media ...

  9. Python 自学积累(二)

    1. onfigParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数( ...

  10. mysql 选择优化的数据类型

    选择最小的数据类型,因为它们占更少的磁盘,内存和CPU缓存: 选择简单的数据类型,如用整型来存储ip: http://blog.csdn.net/lyd518/article/details/2070 ...