【LeetCode】002 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题解:
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 12 ListNode dummy(-1); 13 ListNode* pre = &dummy; 14 int carry = 0; 15 16 while (l1 || l2) { 17 int addend1 = l1 == nullptr ? 0 : l1->val; 18 int addend2 = l2 == nullptr ? 0 : l2->val; 19 int sum = addend1 + addend2 + carry; 20 ListNode* cur = new ListNode(sum % 10); 21 pre->next = cur; 22 pre = pre->next; 23 carry = sum / 10; 24 25 if (l1) 26 l1 = l1->next; 27 if (l2) 28 l2 = l2->next; 29 } 30 31 if (carry) { 32 ListNode* cur = new ListNode(carry); 33 pre->next = cur; 34 } 35 return dummy.next; 36 } 37 };
转载于:https://www.cnblogs.com/Atanisi/p/6713957.html
【LeetCode】002 Add Two Numbers相关推荐
- 【LeetCode】2. Add Two Numbers
传送门:https://leetcode.com/problems/add-two-numbers/ 一.题目描述 You are given two non-empty linked lists r ...
- 【leetcode】258. Add Digits
题目如下: 解题思路:题目很简单,没啥说的.Follow up 我还没想出来. 代码如下: class Solution(object):def addDigits(self, num):" ...
- 【leetcode】41. First Missing Positive
题目如下: 解题思路:这题看起来和[leetcode]448. Find All Numbers Disappeared in an Array很相似,但是有几点不同:一是本题的输入存在负数,二是没有 ...
- 【LeetCode】#39组合总和(Combination Sum)
[LeetCode]#39组合总和(Combination Sum) 加粗样式 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数 ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【Leetcode】103. 二叉树的锯齿形层次遍历
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】102. 二叉树的层次遍历
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3/ \9 20/ \15 7 返回其层 ...
最新文章
- Freemarker整合Spring
- vue输入框联想词功能
- 量子计算101:浅谈其需求、前景和现实
- asp.net core webapi项目配置全局路由
- 一条语句判断数x是否2的n次幂.求取二进制1的个数
- leetcode 65. 有效数字(正则表达式)
- 3-3numpy:向量与矩阵的计算,矩阵的逆
- 新年+情人节礼物,WinDBG找出你内存溢出的地方
- 【字典树】添加和查找单词
- Linux下飞鸽传书项目设计书,Linux 下飞鸽传书设计实现
- BZOJ 1087 SCOI2005 互不侵犯King 状压DP
- javascript无限弹窗_html恶搞之无限弹窗
- Android .apk逆向工程(安装篇):如何正确使用dex2jar
- 如何看一份DBC文件
- 《塞尔达传说》系列游戏评测
- R数据分析:用lme4包拟合线性和非线性混合效应模型
- 苹果手机中病毒显示无服务器,苹果手机浏览器提示中毒,如图?
- 《早秋客舍》赏析-[唐]杜牧古诗
- csdn博客更换皮肤
- 密码学入门(5):单向散列函数
热门文章
- helm部署SkyWalking
- mongodb 实现点赞功能
- Python3 使用推导式统计字符出现次数
- java 自旋锁_java锁的种类以及辨析(一):自旋锁
- 如何画功能稳定,美观的PCB?
- messenger android 4.,AndroidIPC机制(4)-Messenger
- python cv2 matchtemplate_OpenCV-Python系列十:模板匹配
- eigen3.3.8帮助文档下载 chm_MAXHUB文档v1.10.1-MAXHUB文档电脑版下载
- redhat linux ls ls,Linux(3)RedHat7 基本命令二-ls命令詳解
- mysql 回滚失败_浅析Mysql 数据回滚错误的解决方法