LeetCode2——两数相加
题干
给你两个非空的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储一位数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例1:
输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
自己的思路
遍历两个链表的长度,两个都得非空,相加的值 = (num1.val + num2.val + carry) % 10, 进位carry = (num1.val + num2.val + carry) / 10。值得注意的点在于最后有个进位需要单独处理一下。时间复杂度为O(max(l1, l2))
自己写的函数:缺点在于代码不够美观,使用了三个while,代码重复量大,最关键的是多了一个ListNode的存储空间,造成空间浪费。
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) {
int carry = 0; // 进位
ListNode* present = new ListNode();
ListNode* begin = present;
while (l1 != nullptr && l2 != nullptr) { // 两个数均没到结尾
int sum = l1->val + l2->val + carry;
int units = sum % 10;
carry = sum / 10;
ListNode* num = new ListNode(units);
present->next = num;
present = num;
l1 = l1->next;
l2 = l2->next;
}
while (l1 != nullptr) { // l1没到结尾
int sum = l1->val + carry;
int units = sum % 10;
carry = sum / 10;
ListNode* num = new ListNode(units);
present->next = num;
present = num;
l1 = l1->next;
}
while (l2 != nullptr) { // l2没到结尾
int sum = l2->val + carry;
int units = sum % 10;
carry = sum / 10;
ListNode* num = new ListNode(units);
present->next = num;
present = num;
l2 = l2->next;
}
if (carry > 0) { // 处理最后的进位
ListNode* num = new ListNode(carry);
present->next = num;
}
return begin->next;
}
};
看题解更新过后的答案:优点在于通过三元运算符巧妙的避开了写三个while导致代码重复的尴尬,此外在处理上,只有在个位计算完成后才开始创建头部第一个指针,省去了多浪费一个ListNode的空间。
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0; // 进位
ListNode* present = nullptr;
ListNode* head = nullptr;
while (l1 || l2) { // 检测结尾
int n1 = l1 ? l1->val : 0;
int n2 = l2 ? l2->val : 0;
int sum = n1 + n2 + carry;
carry = sum / 10;
if (!head) { // 计算结果
head = present = new ListNode(sum % 10);
}else{
present->next = new ListNode(sum % 10);
present = present->next;
}
if (l1) // 更新指针
l1 = l1->next;
if (l2)
l2 = l2->next;
}
if (carry > 0) {
present->next = new ListNode(carry);
}
return head;
}
};
测试代码
#include "Solution.cpp"
#include <cstdio>
#include <vector>
using namespace std;
ListNode* ChangeVectorToList(vector<int> num) {
ListNode* present = nullptr;
ListNode* head = nullptr;
for (int i = 0; i < num.size(); i++) {
if (!head) {
head = present = new ListNode(num[i]);
}
else {
present->next = new ListNode(num[i]);
present = present->next;
}
}
return head;
}
int main(void) {
Solution solution;
vector<int> num1 = { 9,9,9,9,9,9,9 };
vector<int> num2 = { 9,9,9,9 };
ListNode* begin1 = ChangeVectorToList(num1);
ListNode* begin2 = ChangeVectorToList(num2);
ListNode* result = solution.addTwoNumbers(begin1, begin2); // 两数相加
while (result != nullptr){
printf("%d,", result->val);
result = result->next;
}
return 0;
}