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. struts2 入门程序

    1.struts 2.5.2 基本jar包 2.web.xml <!-- Filters --> <!-- START SNIPPET: filter --> <filt ...

  2. awk基础02-变量-分隔符-数组

        对任意一门语言都会有变量,在awk中变量分为内置变量和自定义变量. 内置变量:就是预先在awk中定义好的,用户可以直接使用 自定义变量:这种变量为用户自己定义的变量,需要先定义后再使用. 内置 ...

  3. 深海划水队项目---七天冲刺day1

    团队会议: 团队成员的任务认领安排: 张兆敏:登录界面,游戏界面(包含游戏区.控制区.显示信息区). 乐滔:方块的形状.移动(向左或者向右)旋转(顺时针或者逆时针),方块的下落(包含硬着陆和软着陆), ...

  4. Oracle电子商务套件版本12.1.3自述文件 (Doc ID 1534411.1)

    文档内容 用途 适用范围 详细信息   应用版本更新包   更新后的步骤   包含的修补程序列表   变更记录   文档可访问性 参考 适用于: Oracle Applications DBA - 版 ...

  5. Android-有序广播明确指定接收者

    在上一篇博客,Android-有序广播是可以中断的,介绍了 有序广播是可以中断的,但还有一种例外情况:明确指定接收者的有序广播是无法中断的,一定会发送到指定的接收者 AndroidManifest.x ...

  6. LINQ to Entities 查询中的标准查询运算符

    投影和筛选方法 投影指的是转换的结果集到所需的窗体中的元素. 例如,可以从结果集中的每个对象投影所需的属性子集,可以投影一个属性并对其执行数学计算,也可以从结果集投影整个对象. 投影方法有 Selec ...

  7. unity 序列化和反序列化

    什么是序列化和反序列化(1)序列化是指把对象转换为字节序列的过程,而反序列化是指把字节序列恢复为对象的过程:.  (2)序列化:对象序列化的最主要的用处就是在传递和保存对象的时候,保证对象的完整性和可 ...

  8. C# 时间戳与DateTime间的互相转换

    //DateTime转换为时间戳public long GetTimeSpan(DateTime time) { DateTime startTime = TimeZone.CurrentTimeZo ...

  9. Oracle数据库02

    EXISTS子查询 特征:将主查询中的数据带到子查询中进行验证,如果验证成功则子查询返回true,当主查询接收到true的时候被验证的数据就显示,如果在子查询中验证失败则返回false,当主查询接收到 ...

  10. java学习笔记—实现一个类MyInputStream(28)

    1 实现一个类MyInputStream读取文件,且不能抛出异常 public class TestDemo { public static void main(String[] args) thro ...