2. Add Two Numbers(2个链表相加)
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个链表相加)的更多相关文章
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [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 ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- 2.Add Two Numbers-两个单链表相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 445 Add Two Numbers II 两数相加 II
给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表.你可以假设除了数字 0 之外,这两个数字都不会以零开头.进阶:如果输入链表 ...
随机推荐
- 1-0 superset的安装和配置
Superset安装及教程官网(http://airbnb.io/superset/installation.html)讲解的已经够详细的了,本篇以官网教程为蓝本进行说明. 入门 Superset目前 ...
- c++11——模板的细节改进
c++11改进了编译器的解析规则,尽可能的将多个右尖括号(>)解析为模板参数结束符,方便编写模板相关的代码. 1. 模板的右尖括号 之前的c++标准中,模板套模板中右尖括号不能连在一块,否则会和 ...
- Java中DESKeySpec类
此类位于 javax.crypto.spec 包下.声明如下: public class DESKeySpec extends Object implements KeySpec 此类指定一个 DES ...
- rpm方式安装 gitlab centos7
一.使用RPM安装 - 推荐 官方推荐的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/ Tips 1 : Centos 7使用el7 ...
- Array.prototype.forEach数组遍历
forEach是Array新方法中最基本的一个,就是遍历,循环.先看以前是怎么遍历数组的 常用遍历 var arr = [1,2,3,4,5]; for(var i = 0; i < arr.l ...
- js中的颜色对应的常量代码code
颜色的对照表 颜色 英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 ...
- 170717、springboot编程之mybatis数据库开发和aop拦截
一.springboot整合mybaits (1)新建maven project; 新建一个maven project,取名为:spring-boot-mybatis (2)在pom.xml文件中引入 ...
- PL/SQL编程基础(五):异常处理(EXCEPTION)
异常处理 异常产生所带来的问题: 使用EXCEPTION程序块进行异常处理: 实现用户自定义异常. 使用异常可以保证在程序中出现运行时异常时程序可以正常的执行完毕: 用户可以使用自定义异常进行操作. ...
- Ora-1157 ora-1110错误解决案例一枚
1.数据库打开报错如下: SQL> alter database open; alter database open * ERROR at line 1: ORA-01157: cannot i ...
- thinkphp中 volist循环的 mod取值的问题
<ul> <volist name="data" id="arr" key="k" mod="2"&g ...