1.题目描述

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

2.我的分析思路

拿到题目,我的第一个思路是这样的:

两个链表,当链表均不为空时,将链表push到栈中,然后同时pop出来,计算栈中数字之和;计算完成后,判断将和对10求余数,放到结果的头结点中,然后把商push到栈中,然后将原始两个链表的值的next赋值为原来的两个链表。

如此递归,即可求出最终值。

不过这里面的判断方式有些问题,比如递归的条件,应该是栈不为空,或者原始的两个链表不为空。

写的代码比较冗余,就不献丑了。

3.其他的思路

现在贴出赞比较多的一个解。

public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode prev = new ListNode(0);
ListNode head = prev;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
ListNode cur = new ListNode(0);
int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
cur.val = sum % 10;
carry = sum / 10;
prev.next = cur;
prev = cur;
l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next;
}
return head.next;
}

这里没有使用到栈的概念,增加了一个carry,也就是表示商。同时,这里面有个概念,java到底是传值和传引用,这里的head和prev就是这样。

LeetCode-2. Add Two Numbers(链表实现数字相加)的更多相关文章

  1. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

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

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

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

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

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

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

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

  6. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  7. leetcode 2 Add Two Numbers(链表)

    数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...

  8. [LeetCode]2. Add Two Numbers链表相加

    注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...

  9. [leetcode]445. Add Two Numbers II 两数相加II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

随机推荐

  1. 修改VS中的附加依赖项的继承值

    工程用不到的库,想去都去不掉,一直链接错误... 解决方法:打开vs的“属性管理器”窗口.通过这个窗口就可以对里面的继承值进行编辑了 另,“属性管理器”这个窗口,一般在“其他窗口”选项里(至少VS20 ...

  2. Oracle Inventory Management Application Program Interface ( APIs) (Doc ID 729998.1)

    In this Document Goal Solution References APPLIES TO: Oracle Inventory Management - Version 12.0.0 a ...

  3. Android-FileUtils-工具类

    FileUtils-工具类 是对文件操作处理的方法进行了封装,提供了公共的方法: package common.library.utils; import android.annotation.Sup ...

  4. java-01 JAVA三大版本比较&JDK、JRE、JVM 的关系

    1. java三大版本比较 1.1 java SE javaSE 是java标准版的简称,其定位是个人计算机应用(应用原生界面比较ugly) 全称:Java Platform Standard Edi ...

  5. Jenkins Pipeline+sonar构建质量平台

    前提: Jenkins JDK 目录: 1.安装sonar插件:SonarQube Scanner for Jenkins 2.安装SonarQube 3.安装sonar-scanner ++++++ ...

  6. C#获取手机验证码+榛子云平台

    今天给大家推荐一个简单的获取手机验证码注册+获取随机4位数 测试框架:.net4.7以上 1.榛子云注册:http://smsow.zhenzikj.com/,送一条测试短信,最低充10元即刻使用 2 ...

  7. Git 常用知识

    git 常用命令 创建并checkout分支: git checkout -b branch_name git merge 与 git rebase 的区别 git rebase 合并后将形成一条直线 ...

  8. 未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf116

    最近项目新增需求批量通过Excel导入数据,果断想到NPOI,结果导入的时候突然跳出 未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, C ...

  9. DOS文件操作命令

    内部命令 COPY---文件固执命令 格式:COPY [源盘:][路径]<源文件名> [目标盘][路径]<目标文件名> 拷贝一个或多个文件到指定盘上 1)COPY是文件对文件的 ...

  10. linux shell中"2>&1"含义

    在计划任务中经常可以看到.例如我们公司的计划任务举例: */ * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task ...