// 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. 将Web站点由IIS6迁移至IIS7

    最近开始着手逐步将所有的Web站点由Win2003+IIS6迁移至64位Win2008+IIS7,基本还算顺利.这里就把相关内容整理总结一下.首先自然是要安装基本运行环境,包括iis,.net fra ...

  2. 查询oracle连接数 对应的 应用程序

     select  b.MACHINE, b.PROGRAM , count(*) from v$process a, v$session b where a.ADDR = b.PADDR and  b ...

  3. C连接MySQL数据库开发之Windows环境配置及测试(转)

    http://blog.csdn.net/xyang81/article/details/26814633(转)

  4. STL容器

    啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...

  5. ACM知识点

    基础算法 高精 模拟 分治 贪心 排序 DFS 迭代加深搜索 BFS 双向BFS 动态规划 DAG上DP 树上DP 线性DP 图算法 最短路 FLYD DJATL BF 最大流 Dinic ISAP ...

  6. js拖动层原形版

    脚本文件: function JzDrag(moveDivId, moveDivHandle) { // var me = this; this.M = false; //是否在移动对象 this.D ...

  7. 判断是手机还是PC端访问

    /*判断手机*/ //判断是手机浏览还是pc //平台.设备和操作系统 var system = { win: false, mac: false, xll: false, ipad: false } ...

  8. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. Unity已经学会的

    1.以MonoBehaviour为脚本的开发模式. 2.MonoBehaviour的大多数API. 3.动画系统大多数了解. 4.UI了解一些,能写UI. 5.Editor了解一些,能写Editor. ...

  10. 跨境B2B网站

    大家都在谈跨境电商,其实现在比较火的应该是跨境B2B网站,它被很多的业内人士所看好,并且也取得了很喜人的成绩,无论是经营方面还是品牌打造,都从多方向带动了行业的发展. 跨境B2B网站 一.从买方市场向 ...