问题
中文版
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
提示:
每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
题目数据保证列表表示的数字不含前导零
英文版
2. 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 contains a single digit.
Add the two numbers and return the sum as a linked list.
You may assume the two numbers
do not contain any leading zero,
except the number 0 itself.
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]
Constraints:
The number of nodes in each linked list is
in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list
represents a number that does not have leading zeros.
通过次数1,545,776提交次数3,667,142
代码
小白写的
#include <iostream>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *l3 = new ListNode;
ListNode *head1 = new ListNode, *head2 = new ListNode, *head3 = new ListNode;
head1 = l1, head2 = l2, head3 = l3;
for (; ; ) { // l1->next != nullptr || l2->next != nullptr
if (l1 != nullptr && l2 != nullptr) {
l3->val += l1->val + l2->val;
if (l3->val > 9) {
l3->next = new ListNode;
l3->next->val += l3->val / 10;
l3->val = l3->val % 10;
}
if (l1->next != nullptr || l2->next != nullptr) {
if (l3->next == nullptr)
l3->next = new ListNode;
l3 = l3->next;
}
l1 = l1->next;
l2 = l2->next;
}
else if (l1 == nullptr && l2 != nullptr) {
l3->val += l2->val;
if (l3->val > 9) {
l3->next = new ListNode;
l3->next->val += l3->val / 10;
l3->val = l3->val % 10;
}
if (l2->next != nullptr) {
if (l3->next == nullptr)
l3->next = new ListNode;
l3 = l3->next;
}
l2 = l2->next;
}
else if (l1 != nullptr && l2 == nullptr) {
l3->val += l1->val;
if (l3->val > 9) {
l3->next = new ListNode;
l3->next->val += l3->val / 10;
l3->val = l3->val % 10;
}
if (l1->next != nullptr) {
if (l3->next == nullptr)
l3->next = new ListNode;
l3 = l3->next;
}
l1 = l1->next;
}
else if (l1 == nullptr && l2 == nullptr){
break;
}
}
return head3;
}
};
int main() {
/*
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
*/
Solution sol;
ListNode *l1, *l2, *l3, *head1, *head2, *head3;
l1 = new ListNode;
head1 = new ListNode;
head1 = l1;
l2 = new ListNode;
head2 = new ListNode;
head2 = l2;
l3 = new ListNode;
head3 = new ListNode;
head3 = l3;
for (int i = 0; i < 3; i++) { // 测试一下,也可以在leetcode测试
l1->val = i + 1; // 3, 2, 1
l2->val = i + 5; // 7, 6, 5
if (i < 2) {
l1->next = new ListNode;
l2->next = new ListNode;
}
l1 = l1->next;
l2 = l2->next;
}
l3 = sol.addTwoNumbers(head1, head2);
head3 = l3;
for (int i = 0; i < 4; i++) {
cout << l3->val;
l3 = l3->next;
}
cout << endl;
return 0;
}
大佬的码
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = nullptr, *tail = nullptr;
int carry = 0; // 用来储存进位值
while (l1 || l2) {
int n1 = l1 ? l1->val: 0; // 取得 l1 节点储存的数值赋给 n1
int n2 = l2 ? l2->val: 0;
int sum = n1 + n2 + carry;
if (!head) { // 判断是头节点是否为 nullptr
head = tail = new ListNode(sum % 10); // tail 一开始指向头节点,头节点也储存值
} else {
tail->next = new ListNode(sum % 10); // tail 是从 head 开始的,添加数据时,需要 为tail->next 申请空间
tail = tail->next; // tail指向已经储存数据的那一项
}
carry = sum / 10; // 更新进位值
if (l1) { // 如果当前 l1, l2 不为空指针,则 ”移动到“ 下一个节点
l1 = l1->next;
}
if (l2) {
l2 = l2->next;
}
}
if (carry > 0) { // 循环结束后判断是否要为 carry 额外开辟一个节点
tail->next = new ListNode(carry);
}
return head;
}
};
知识点
结构体
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};