当前位置:网站首页>【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;}};
边栏推荐
- 市面上有哪些经典的文档数据库呀?
- ASP.NET Core 2.0中,解决大文件上传配置问题
- 四、业务数据解析
- 基于ftp协议的上传与下载
- A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
- Vulnhub靶机:GEMINI INC_ 1
- 小程序使用formdata格式传参
- What is intrinsic safety?
- 键值数据库中的键是什么类型的对象呢?
- 有哪些典型的列存储数据库呢?
猜你喜欢
随机推荐
d实验新异常
PWA 应用 Service Worker 缓存的一些可选策略和使用场景
使用C# 调用api接口获取法定节假日(百度api)
面试突击72:输入URL之后会执行什么流程?
mysql 性能分析
网盘目录搜索系统源码+搭建教程
市面上有哪些经典的文档数据库呀?
Flutter Game Tutorial Recreate the famous T-Rex game with Flutter and Flame
Flutter实现搜索的三种方式
苹果开发者账号申请流程完整版
How to uniformly handle error exceptions in embedded C programming?
dedecms支持Word图文一键导入
A concise tutorial on expanding (increasing capacity) of VMWare Esxi virtual system data storage
NoSQL的意思就是就是不使用SQL吗?
使用文档数据库的目的是什么呢?
持久化键值数据库的数据是保存在内存中吗?
使用ApacheBench来对美多商城的秒杀功能进行高并发压力测试
2022 world conference on robots is holding, intelligent robot booster to intelligent, digital transformation and upgrading traditional industry
「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线
Vulnhub靶机:GEMINI INC_ 1