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.其他的思路

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

  1. public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  2. ListNode prev = new ListNode(0);
  3. ListNode head = prev;
  4. int carry = 0;
  5. while (l1 != null || l2 != null || carry != 0) {
  6. ListNode cur = new ListNode(0);
  7. int sum = ((l2 == null) ? 0 : l2.val) + ((l1 == null) ? 0 : l1.val) + carry;
  8. cur.val = sum % 10;
  9. carry = sum / 10;
  10. prev.next = cur;
  11. prev = cur;
  12. l1 = (l1 == null) ? l1 : l1.next;
  13. l2 = (l2 == null) ? l2 : l2.next;
  14. }
  15. return head.next;
  16. }

这里没有使用到栈的概念,增加了一个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. .NET基础 (08)字符串处理

    字符串处理1 System.String是值类型还是引用类型2 StringBuilder类型有何作用3 如何在String和Byte[]对象之间进行转换4 简述BASE64编码的作用以及C#中对其的 ...

  2. underscore概况

    看的是1.3.3,这个版本的中文源码解释比较多. 函数的中文注释:http://www.css88.com/doc/underscore1.5.2/#difference 源码的中文注释:http:/ ...

  3. opengl教程推荐

    非常不错的教程! OpenGL教程

  4. Configuring Oracle E-Business Suite Integrated SOA Gateway Release 12.1.2 and Release 12.1.3 in a Multinode Environment (Doc ID 1081100.1)

    Configuring Oracle E-Business Suite Integrated SOA Gateway Release 12.1.2 and Release 12.1.3 in a Mu ...

  5. Hibernate 之核心接口

    1.持久化和ORM 持久化是指把数据(内存中的对象)保存到可持久保存的存储设备中(如硬盘),主要应用于将内存中的数据存储到关系型数据库中,在三层结构中,持久层专注于实现系统的逻辑层面,将数据使用者与数 ...

  6. sql查询优化--数字转换字符串字段

    SELECT top 1 pt.* FROM t1where id='20180731223014' SELECT top 1 pt.* FROM t1where id='0180731223014 ...

  7. 好看的table样式

    收藏个好看的table样式 <style type="text/css">table.gridtable { font-family: verdana,arial,sa ...

  8. C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件)

    string file =Application.StartupPath+@"\WinFrm_Main.exe";//运行程序位置 public Form1() { Initial ...

  9. C#导出EXCEL,并生成charts表

    需要添加引用  Microsoft.Office.Interop.Excel 注意:使用Microsoft.Office.Interop.Excel 非常耗时.对性能有要求建议用其他. 如果要用,把数 ...

  10. AppDomain.CurrentDomain.BaseDirectory项目目录相关操作

    链接:https://www.cnblogs.com/guolianyu/p/3980971.html 经常用到,每次都百度,所以自己备份一下!