// 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. linux基础2——gdb调试器

    gdb调试器使用的一般步骤(不断更新完善): 1.编译过程中要用-g参数来添加调试符号——gcc test.c -g: 2.gdb启动可执行文件——gdb a.out: 3.出现gdb符号表示启动成功 ...

  2. C#实用技能篇

    Redis配置文件详解 如果不指定配置文件,redis也可以启动,此时,redis使用默认的内置配置.不过在正式环境,常常通过配置文件[通常叫redis.conf]来配置redis. redis.co ...

  3. win10 MySQL启动失败问题

    系统升级到win10之后,本地装的MySQL却突然不能启动,系统显示明明就有,可是总是启动失败.在这里解决一下: 解决win10  mysql服务消失,连接不上的问题,注意:以管理员身份运行DOS命令 ...

  4. 配置SSH框架的心得

    开发工具为myeclipse,开发工具自带struts2,hibernate,spring框架的导入.在新建的项目上右击Myeclipse->Project Facets->Install ...

  5. Android-----test----monkeyrunner

    1.下载  monkey_recorder.py和monkey_playback.py这两个文件: 2.存放到对应的虚拟机的tools文件夹下,如我的 D:\adt-bundle-windows-x8 ...

  6. JS数组转成json字符串的注意事项

    在js中常常会将一个数组转成json字符串发送给后端. 这时候在定义数组数据结构的时候需要格外注意,意味json中是有集合和对象的区别的. 集合的定义是[];对象的的定义是{}. 这时候,在创建数组时 ...

  7. Media Queries详解

    Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码:  <link href="css/reset.css" rel ...

  8. 关于html自闭合标签要不要加空格和斜杠的问题?

    问题描述:可能很多人都遇到过这个问题,写网页时,link img br input等等这些标签时到底要不要在结尾加上空格和斜杠呢? 我曾经貌似在<编写高质量代码>上看到过这样的介绍,遇到l ...

  9. C#图片上传服务器缩放存储功能

    项目需求上需要用户上传头像.本来是用第三方的插件的.但是很多都是收费的. 不过,我需要花这钱么?是不?所以网络上搜集了些资料.. 果然.这个世界 大神是很多很多的. 用了个免费插件. <scri ...

  10. Tengine

    Tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性. 外文名 Tengine 发起单位 淘宝网 基    础 Nginx 目    ...