当前位置:网站首页>445. adding two numbers II
445. adding two numbers II
2022-06-11 08:55:00 【Drag is me】
leetcode Force button to brush questions and punch in
subject :445. Addition of two numbers II The finger of the sword Offer II 025. The two numbers in the linked list are added
describe : Here are two for you Non empty The list represents two nonnegative integers . The highest digit is at the beginning of the list . They store only one digit per node . Adding these two numbers will return a new linked list .
You can assume that in addition to the numbers 0 outside , Neither of these numbers starts with zero .
Their thinking
1、 Reverse the two linked lists and add them ;
2、 Pay attention to carry jinwei;
3、 Pay attention to judge whether the linked list is empty or has come to the end ;
Source code ##
class Solution {
public:
ListNode *reverseList(ListNode *head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode *pre = nullptr;
ListNode *cur = head;
while (cur) {
ListNode *temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *l11 = reverseList(l1);
ListNode *l12 = reverseList(l2);
ListNode *p = new ListNode(-1);
ListNode *ans = p;
int jinwei = 0;
while (l11 || l12) {
int num1 = l11 ? l11->val : 0;
int num2 = l12 ? l12->val : 0;
int sum = num1 + num2 + jinwei;
p->next = new ListNode(sum % 10);
jinwei = sum / 10;
if (l11) l11 = l11->next;
if (l12) l12 = l12->next;
p = p->next;
}
if (jinwei) p->next = new ListNode(jinwei);
return reverseList(ans->next);
}
};
边栏推荐
- leetcode - 739. 每日温度
- Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘
- 2、 Rendering camera data
- 补2:圆环回原点问题
- 硅树脂油漆申请美国标准UL 790 Class A 合适吗?
- 面試題 02.02. 返回倒數第 k 個節點
- How to apply for ASTM E108 flame retardant test for photovoltaic panels?
- Wood board ISO 5660-1 heat release rate mapping test
- 光伏板怎么申请ASTM E108阻燃测试?
- 马志强:语音识别技术研究进展和应用落地分享丨RTC Dev Meetup
猜你喜欢

标准化编写知识

Matlab学习7-图像处理之线性平滑滤波

复制的代码格式混乱怎么办?

What is the process of en 1101 flammability test for curtains?

The interviewer asked four questions and summed up four experiences

MySQL upgrade

Intelligent control theory question bank

leetcode - 230. 二叉搜索树中第K小的元素

leetcode - 739. Daily temperature

Getting started with Zipkin
随机推荐
For in / for of / foreach loop
哪些Apple设备支持这次系统更新?来看你的旧Apple设备支持最新系统吗
PVC 塑料片BS 476-6 火焰传播性能测定
智能控制理论小题库
445. 两数相加 II
File system check of the root filesystem failed
领导让我重写测试代码,我也要照办嘛?
室内膨胀型防火涂料根据BS 476-21 耐火标准测定需要符合几项?
Printf correlation of C
SAP ABAP data types and data objects
Screaming Frog Log File Analyser 中文版安装教程
Notes on MySQL core points
SAP ODATA 开发教程
Sword finger offer 10- ii Frog jumping on steps
SQL基本查询
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
206. 反转链表
Livedata and stateflow, which should I use?
EN 45545-2:2020 T11 smoke toxicity test
Sword finger offer 40 Minimum number of K