LeetCode-2. Add Two Numbers(链表实现数字相加)
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(链表实现数字相加)的更多相关文章
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
- [LeetCode] 2. Add Two Numbers 两个数字相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [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 ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- LeetCode 445. Add Two Numbers II (两数相加 II)
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...
- leetcode 2 Add Two Numbers(链表)
数字反过来这个没有什么麻烦,就是镜像的去算十进制加法就可以了,然后就是简单的链表. /** * Definition for singly-linked list. * struct ListNode ...
- [LeetCode]2. Add Two Numbers链表相加
注意进位的处理和节点为null的处理 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int flag = 0; ListNode ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
随机推荐
- CSS实现图片阴影效果
<title>无标题文档</title> <style type="text/css"> /*方法一:使用一个GIF文件的方法*/ .gifsh ...
- 团体程序设计天梯赛L2-001 紧急救援 2017-03-22 17:25 93人阅读 评论(0) 收藏
L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国 ...
- java并发编程实战:第十四章----构建自定义的同步工具
一.状态依赖性管理 对于单线程程序,某个条件为假,那么这个条件将永远无法成真 在并发程序中,基于状态的条件可能会由于其他线程的操作而改变 可阻塞的状态依赖操作的结构 acquire lock on o ...
- Python学习-6.Python的分支语句
Python的分支语句比较简单,只有if.else.elif三个关键字,也就是说,Python没有switch语句,而且,Python中并没有?:这个三目运算符. 例子: age = 18 if ag ...
- 史上最详细的Hadoop环境搭建(转)
转载的文章,请告知侵删.本人只是做个记录,以免以后找不到. 前言 Hadoop在大数据技术体系中的地位至关重要,Hadoop是大数据技术的基础,对Hadoop基础知识的掌握的扎实程度,会决定在大数据技 ...
- sql-修改每条数据的某一个字段的值
update B set B.maildata =(select SUBSTRING(maildata,0,3) from basedata where basedata.cid = B.cid)+( ...
- EF一对多的表,模糊查询2个表的数据!
如用户表和电话表,要求搜索时可以模糊查询姓名和号码.都可以找到包含该字符的所有用户. /// <summary> /// 模糊查询姓名和电话号码,并按姓名排序返回 /// </sum ...
- WebApi Post string 参数 为空
用webApi做开发也有很久了 一些 细节平时可能未必很留心 今天就很奇葩 post 只接受一个string 参数的数据 但接收是一直未空 很奇怪 看了一些资料后得出以下结论
- ceph 移除 osd
1:从crush中移除节点ceph osd crush remove osd.0 2:删除节点ceph osd rm osd.0 3:删除节点认证(不删除编号会占住)ceph auth del osd ...
- parseInt/类型转换/字符串
1.pa'rseInt整型 1.1parseInt必须以数字开头的 var topVal = parseInt("28px"); console.log(topVal); 1.2非 ...