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. fgetc read缓冲机制区别

    read属于系统调用,它的缓存是基于内核的缓冲,是记在内核空间的. 而fgetc是标准函数, 是在用户空间I/O缓冲区的 比如用fgetc读一个字节,fgetc有可能从内核中预读1024个字节到I/O ...

  2. EventBus 简单原理(一)

    EventBus 1.根据文章最前面所讲的EventBus使用步骤,首先我们需要定义一个消息事件类: public class MessageEvent { private String messag ...

  3. iOS事件拦截(实现触摸任意位置隐藏指定view)

    项目里有一个需求,类似新浪或者腾讯微博的顶部title栏的类别选择器的消失(在选择器展开的时候,触摸屏幕任何地方使其消失). 最开始的想法是当这个选择器(selectorView)展开的时候,在当前屏 ...

  4. 新版本读取老版本文件崩溃BUG

    读取文件匹配代码 BOOL CWBPage::LoadFromFile(CFile *pFile, LONGLONG& lOff, ULONGLONG lFileLength) { if (p ...

  5. Java语言基本数据类型

    ■Java是一种强类型语言,每个变量都必须声明其类型.■Java的数据类型分为两大类:基本类型和引用类型(引用数据类型的大小统一为4个字节,记录的是其引用对象的地址).■Java中定义了3类8种基本数 ...

  6. Xcode模版生成文件头部注释

    在使用Xcode创建工程或者新建类的时候,顶部都会有一些xcode帮我们生成的注释 //// MySingletonClass.h// 单例模式//// Created by mark on 15/8 ...

  7. Hacking up an armv7s library

    NOTE: Please take care with this. I obviously cannot test if this will actually work on a new iPhone ...

  8. java基础---->多线程之priority(四)

    线程的priority能告诉调度程序其重要性如何,今天我们通过实例来学习一下java多线程中的关于优先级的知识.我从没被谁知道,所以也没被谁忘记.在别人的回忆中生活,并不是我的目的. java多线程的 ...

  9. LeetCode——Basic Calculator

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

  10. maven安装和与IDE集成

    第一部分:maven的基本信息和安装,配置  maven是一个项目构建和管理的工具,提供了帮助管理 构建.文档.报告.依赖.scms.发布.分发的方法.可以方便的编译代码.进行依赖管理.管理二进制库等 ...