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. 九度 1529:棋盘寻宝(递推DP)

    题目描述: 现在有一个8*8的棋盘,上面放着64个价值不等的礼物,每个小的棋盘上面放置一个礼物(礼物的价值大于0小于1000),一个人的初始位置在棋盘的左上角,每次他只能向下或向右移动一步,并拿走对应 ...

  2. Swift-基础语法之变量&常量&元组

    使用 let来声明一个常量,用 var来声明一个变量.常量的值在编译时并不要求已知,但是你必须为其赋值一次.这意味着你可以使用常量来给一个值命名,然后一次定义多次使用 myVariable = let ...

  3. Python 爬虫知识点

    一.基础知识 1.HTML分析 2.urllib爬取 导入urilib包(Python3.5.2) 3.urllib保存网页 import urllib.requesturl = "http ...

  4. combobox组合框

    最近在改BUG的时候发现,combobox组合框如果选择的是Dropdown模式在初始化combobox对象时候有如下操作 1.SetDlgItemInt(IDC_WB_FONTSIZECOMBOX, ...

  5. 好用的在线Markdown编辑器

    dillinger.io 支持在线编辑,导出为html格式等.

  6. 【jdk源码学习】HashMap

    package com.emsn.crazyjdk.java.util; /** * “人”类,重写了equals和hashcode方法...,以id来区分不同的人,你懂的... * * @autho ...

  7. luogu P2066 机器分配[背包dp+方案输出]

    题目背景 无 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15 ...

  8. linux 上安装pstree

    linux 无法使用pstree centos7上默认没有安装psmisc包. 1.在 Mac OS上 brew install pstree 2.在 Fedora/Red Hat/CentOS yu ...

  9. 处理URL传递中文乱码问题

    在网上搜了很多资料都没有搞定,一般都有以下几种说法: 方法1:在后台中先获得字符串的iso-8859-1编码形式数组,再使用此数组实例一个UTF-8编码形式String类型字符串. 页面提交的url为 ...

  10. 【css预处理器】------css预处理器及sass基本介绍------【巷子】

    001.什么是css预处理器? css预处理器定义了一种新的语言.用一种专门的编程语言,为css增加了一些编程的特性,将css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作.(通俗点说“” ...