Leetcode: 2. Add Two Numbers
一直想用一个进位变量来存贮进位值,但老是考虑不周全,下面是我自己写的bug代码,考虑不周,因为l1或者l2都有可能为null
class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode listEnd = new ListNode(0);ListNode list = listEnd;int carry = 0;while (l1 != null || l2 != null) {int sum = 0;if(l1!=null) {sum =carry+l1.val;l1 = l1.next;}if(l2!=null) {sum =l2.val+sum;l2 = l2.next;}list.next = new ListNode(sum % 10);carry = sum/10;list = list.next;}if (carry== 1)list.next = new ListNode(1);return listEnd.next;} }
acccpt:https://discuss.leetcode.com/topic/799/is-this-algorithm-optimal-or-what
import java.util.Map;public class Test2 {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode listEnd = new ListNode(0); //作为最后返回的链表,返回的是nextListNode list = listEnd; //一直在遍历的链表,因为list指针一直是往后移动,所以只用一个指针是完成不了任务的,必须有一个作为缓存int sum = 0;while (l1 != null || l2 != null) {sum =sum/10; //求进位,这里就只能用这种方式。我一直在想用一个变量来存储,但往往会考虑不周if(l1!=null) {sum =sum+l1.val;l1 = l1.next;}if(l2!=null) {sum =sum+l2.val;l2 = l2.next;}list.next = new ListNode(sum % 10); //动态产生链表节点 list = list.next;}if (sum / 10 == 1) //最后的和大于10的话,则进位list.next = new ListNode(1);return listEnd.next;} }
转载于:https://www.cnblogs.com/Michael2397/p/8025059.html
Leetcode: 2. Add Two Numbers相关推荐
- LeetCode之Add Two Numbers
LeetCode之Add Two Numbers 题目:You are given two linked lists representing two non-negative numbers. Th ...
- LeetCode 445. Add Two Numbers II
LeetCode 445. Add Two Numbers II Solution1:我的答案 利用了栈,这样就不用翻转链表了... /*** Definition for singly-linked ...
- 【注意】LeetCode 2. Add Two Numbers
LeetCode 2. Add Two Numbers 这种沙比提怎么都写不对了??? Solution1:学习这种写法 /*** Definition for singly-linked list. ...
- LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法
题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked lists representing two non-neg ...
- [Leetcode] 445. Add Two Numbers II
问题: https://leetcode.com/problems/add-two-numbers-ii/#/description 思路:该题与"415. Add Strings" ...
- LeetCode:2. Add Two Numbers
https://leetcode.com/problems/add-two-numbers/description/ 内容描述 You are given two non-empty linked l ...
- LeetCode 2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- leetcode 【 Add Two Numbers 】 python 实现
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- leetcode 2. Add Two Numbers | 2. 两数相加(Java)
题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 需要注意 当两个链表不一样长时 的收尾处理. /*** Definition for ...
最新文章
- GCC 连接器、链接标准库 gcc -l、链接手动创建库(指定目录的库 gcc -L)
- 怎么快速把语音转成文字
- 第四周项目五-用递归方法求解(求n的阶乘)
- Linux2.6中的Slab层
- 多维数组的索引与切片_「GCTT 出品」Go 系列教程——11. 数组和切片
- ubuntu如何更改用户名和密码
- C# 之不安全代码(2)
- switch和toggle在软件开发中的含义和区别
- 一键装机linux_(推荐)linux用一键安装包
- python函数——形参中的:*args和**kwargs
- linux suse最新版本,SUSE Linux Enterprise Server 15 正式发布
- 直播app开发怎么做,PHP直播源码是什么
- AdventureWorks2008R2安装过程可能会遇到的一些问题及解决方案
- 基因家族鉴定分析实战操作手册
- Android技能树 — 动画小结
- Centos6 密钥登陆,解决所选的用户密钥未在远程主机上注册
- java导出多个excel表格,并压缩成zip输出
- Android App包瘦身优化
- (附源码)计算机毕业设计SSM旅游推广系统
- 临沂大学计算机考研资料汇总