// Java
     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
         return add(l1, l2, false);
     }

     private ListNode add(ListNode l1, ListNode l2, boolean step) {
         if (l1 == null && l2 == null && !step)
             return null;
         int i = l1 != null ? l1.val : 0;
         int j = l2 != null ? l2.val : 0;
         ListNode result;
         if (step) {
             result = new ListNode((i + j + 1) % 10);
             step = i + j + 1 >= 10;
         } else {
             result = new ListNode((i + j) % 10);
             step = i + j >= 10;
         }
         result.next = add(l1 == null ? null : l1.next,
                 l2 == null ? null : l2.next, step);
         return result;
     }

     public class ListNode {
         int val;
         ListNode next;
         ListNode(int x) { val = x; }
     }

2 Add Two Numbers的更多相关文章

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

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

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

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

  3. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  4. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  5. [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 ...

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

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

  7. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  8. [LeetCode_2] Add Two Numbers

    LeetCode: 2. Add Two Numbers /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  9. Two Sum & Add Two Numbers

    Two Sum 题目:https://leetcode.com/problems/two-sum/ class Solution(object): def twoSum(self, nums, tar ...

  10. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

随机推荐

  1. Ajaxtoolkit Combobox位置显示不对解决

    当父级容器设置positon是absolute或relative,combobox显示的就会出现偏移,解决办法如下: 添加这个样式 .ajax__combobox_itemlist{    posit ...

  2. java写入excel文件poi

    java写入excel文件 java写入excel文件poi,支持xlsx与xls,没有文件自动创建 package com.utils; import java.io.File; import ja ...

  3. ES6新特性之模板字符串

    ES6新特性概览  http://www.cnblogs.com/Wayou/p/es6_new_features.html 深入浅出ES6(四):模板字符串   http://www.infoq.c ...

  4. CentOS 7部署OpenStack(二)—安装keystone服务

    1.创建数据库 [root@controller ~]# mysql -u root -p [root@controller ~]# CREATE DATABASE keystone; [root@c ...

  5. sublime 工具构建

    1 Sublime Text 3 配置react语法校验 原文地址:https://segmentfault.com/a/1190000004369542?_ea=585496 终端安装 npm in ...

  6. Java集合框架(常用类) JCF

    Java集合框架(常用类) JCF 为了实现某一目的或功能而预先设计好一系列封装好的具有继承关系或实现关系类的接口: 集合的由来: 特点:元素类型可以不同,集合长度可变,空间不固定: 管理集合类和接口 ...

  7. centos 7 相关的一些记录

    开80端口: /tcp --permanent 重新加载防火墙: sudo firewall-cmd --reload 安装nginx: sudo rpm -Uvh http://nginx.org/ ...

  8. SQL计算时间差,要精确到小时分钟秒

    declare @starttime as datetime declare @endtime as datetime set @starttime = '2009-11-24 15:10:00' s ...

  9. 数据库FMDB-sql

    1.首先要先导入第三方类库FMdatabase. 2.获得存放数据库文件的沙盒地址. #pragma mark - 创建数据库 - (void)createAndInitDatabase { NSSt ...

  10. 用CMake构建Qt5的Visual Studio工程

    使用Visual Studio构建Qt工程的方法有很多种,可以使用Visual Studio自带的功能手动创建配置工程,也可以创建pro文件,然后通过VS的Qt插件导入进行创建.还有一种方式是通过CM ...