*LeetCode--Add Two Numbers
Add Two Numbers
两数之和,本位和进位的求和
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.
Example
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {if(l1 == null || l2 == null) return null;ListNode sum = new ListNode(0);ListNode node = sum;int num = 0;int carry = 0;while(l1 != null || l2 != null){ if(l1 != null) {num += l1.val;l1 = l1.next;}if(l2 != null) {num += l2.val;l2 = l2.next;}carry = num / 10;node.next = new ListNode(num % 10);node = node.next;num = carry;}if(num != 0) node.next = new ListNode(num);return sum.next;}
}
转载于:https://www.cnblogs.com/SkyeAngel/p/9083727.html
*LeetCode--Add Two Numbers相关推荐
- LeetCode Add Two Numbers II(栈)
问题:给出两个链表表示的整数,求其和 思路:因为链表的第一个结点是最高位,最后一个结点是最低位.先将两个链表放入两个栈中.然后从两个栈中取出元素,从低到高位相加. 具体代码参考: https://gi ...
- 每日一则 LeetCode: Add Two Numbers
描述 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- LeetCode:Add Two Numbers
题目链接 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- [LeetCode] Add Two Numbers
题目链接: https://oj.leetcode.com/problems/add-two-numbers/ 问题: You are given two linked lists represent ...
- leetcode:Add Two Numbers(java)
package Leetcode;/*** 题目:* You are given two non-empty linked lists representing two non-negative in ...
- [LeetCode] Add Two Numbers(stored in List)
首先,演示一个错误的reverList 1 class Solution { 2 public: 3 ListNode* reverse(ListNode* root) 4 { 5 if(NULL = ...
- LeetCode Add Two Numbers
题意:链表加法 代码如下: class Solution {ListNode addTwoNumbers(ListNode l1, ListNode l2){ListNode cur1 = l1, c ...
- leetcode add Two Numbers
部分 conditional operators ?:写的statements 在有的编译器下能通过,有的可能通不过 base operand of '->' has non-pointer ...
- 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" ...
最新文章
- 简单的TableViewCell高度自适应(只有Label,仅当参考思路)
- 字节数与字符数mysql_数据库字段长短之字节与字符
- web安全漏洞之CSRF
- 整个html和内部html,什么是HTML?
- 安装一个插件、回馈给你一款属于猿的绚丽浏览器
- 百度地图离线sdk java_百度地图SDK java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapSDK...
- Python 用for循环实现猜数字游戏
- GNU make manual 翻译(三十一)
- 一种低侵入性的组件化方案 之 组件化需要考虑的几个问题
- CryptoJS加密
- [源码解读]position_estimator_inav_main解读(如何启动光流)
- 你的终极目标是什么?
- 2020年重磅喜讯!热烈祝贺王家林大咖人工智能及大数据领域经典著作《Apache Spark+AI全息代码解密》清华大学出版社发行上市!
- 大数据人工智能行业技术图谱
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(many-to-one unique=true、one-to-one)...
- 四参数与七参数坐标转换含义及区别
- 「科普」一文读懂生产制造MES系统
- 荣耀手机和小米打出了真火,针锋相对比拼千元机
- NVT | NVT 67X IQ移植
- Excel如何对一首韩文歌曲进行中文翻译?
热门文章
- HDOJ 1202 The calculation of GPA
- 如何判断应用已经安装,如何判断Service,BroastCastReceiver,ContentProvider是否存在...
- Maven使用教程(转)
- js 调用C#.NET后台方法 转载自:http://www.cnblogs.com/lizhao/archive/2010/11/23/1990436.html...
- 使用Laya引擎开发微信小游戏(下)
- PHP类继承、接口继承关系概述
- [转]Apache Commons IO入门教程
- Symantec Backup Exec Remote Agent 2010在Redhat Enterprise 6.6上启动问题
- Singleton模式学习
- 在集群环境中安装sql2005的sp2