当前位置:网站首页>【Force】Add two numbers
【Force】Add two numbers
2022-08-08 10:33:00 【Town house to solve the problem!】
【Letcode】Add two numbers
Question link
Title description
gives you two non-empty linked lists representing two non-negative integers.They are stored in reverse order per digit, and each node can only store one digit.
Please add two numbers and return a linked list representing the sum in the same form.
You can assume that neither number starts with a 0 except for the number 0.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Hint:
The number of nodes in each linked list is in the range [1, 100]
0 <= Node.val <= 9
The title data guarantee list representationof numbers without leading zeros
AC Code
class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {auto head =new ListNode(-1);auto cur = head;int t = 0;while(l1 || l2 || t){if(l1) t += l1->val, l1 = l1->next;if(l2) t += l2->val, l2 = l2->next;cur = cur->next = new ListNode(t % 10);t /= 10;}return head->next;}};边栏推荐
猜你喜欢
随机推荐
图数据库是使用什么作为数据模型的呢?
新款“廉价”SUV曝光,安全、舒适一个不落
关系型数据库的优缺点是什么?
Timed Task Framework Quartz-(1) Quartz Introduction and Demo Construction
TCP通信
Feign application and source code analysis
Categorized input and output, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, go lang basic data types and input and output EP03
nacos安装
2022世界机器人大会即将举办,智能机器人助推传统行业向智能化、数字化转型升级
高并发下秒杀商品,你必须知道的9个细节
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
八、排序与搜索
利用图像二维熵实现视频信号丢失检测(Signal Loss Detection)
简单混合运算计算器
go web之响应用户
开源一夏 | 牛plus,多层嵌套动态JSON该如何解析总结
【数学知识】—— 质数/约数
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
面试突击72:输入URL之后会执行什么流程?
左益豪:用代码创造一个新世界|OneFlow U









