// 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. Jstl简单应用

    jsp引入信息------ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % ...

  2. 九、数据库——sql server 2008导入excel

    昨天分配给我一个活,让我手动录入新闻网页的数据,包括每条新闻的标题.时间和链接. 一开始,就是按照最原始的手动录入的方法,一条条的录入.发现这简直就是在浪费时间,于是就想了一种新方法. 1.将网页中的 ...

  3. 2.2 ARM处理器工作模式

    ARM Architecture Reference Manual Arm 指令框架手册 种工作模式 Processor mode Mode number Description User usr 0 ...

  4. Oracle和SQLServer解锁杀进程

    ORACLE:在平时工作中经常会遇到数据库死锁的情况,以前使用oracle时候(那时候还不懂),出现这种情况时前辈给了我一段命令:SELECT dob.OBJECT_NAME Table_Name,l ...

  5. nginx 使用

    1.下载nginx包http://files.cnblogs.com/files/jyjin/nginx.zip 2.解压后找到nginx.conf文件进行配置 3.配置server选项: serve ...

  6. javascript call与apply关键字的作用

    apply接受两个参数.第一个参数指定函数体内this对象的指向,第二个参数为一个带下标的集合. call则是apply的语法糖,如果参数数量固定,则可以不用带下标的集合传第二个参数. 1 2 3 4 ...

  7. js 表格实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. C语言中内存操作函数

      一.malloc/calloc 名称: Malloc/calloc 功能: 动态内存分配函数 头文件: #include <stdlib.h> 函数原形: void *malloc(s ...

  9. AKKA(一)认知AKKA

    Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用.它已经成功运用在电信行业.系统几乎不会宕机(高可用性 99.999999 ...

  10. C语言隐形密码输入

    今天费了老大的劲,终于做出来了!虽然简单,但也是自己的心血,分享一下! #include<stdio.h> #include<conio.h> int main(){ char ...