2021-12-25
两个数相加
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
【解析】
本题和上一题二进制数相加是一样的,区别只是在于上一题是基于数组的,而本题是基于链表的。把数组操作改为链表操作即可。
【code】
/** * 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) {} * }; */
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = nullptr, *tail = nullptr;
int carry = 0;
while (l1 || l2) {
int n1 = l1 ? l1->val: 0;
int n2 = l2 ? l2->val: 0;
int sum = n1 + n2 + carry;
if (!head) {
head = tail = new ListNode(sum % 10);
} else {
tail->next = new ListNode(sum % 10);
tail = tail->next;
}
carry = sum / 10;
if (l1) {
l1 = l1->next;
}
if (l2) {
l2 = l2->next;
}
}
if (carry > 0) {
tail->next = new ListNode(carry);
}
return head;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *result = new ListNode(-1);
ListNode *pre = result;
ListNode *pa = l1->next, *pb = l2->next;
int carry = 0;
while (pa != NULL || pb != NULL){
int av = pa == NULL ? 0 : pa->val;
int bv = pb == NULL ? 0 : pb->val;
//cout << "av:" << av<<" bv: "<<bv<<endl;
ListNode *node = new ListNode((av + bv + carry) % 10);
carry = (av + bv + carry) / 10;
pre->next = node;
pre = pre->next;
pa = pa == NULL ? NULL : pa->next;
pb = pb == NULL ? NULL : pb->next;
}
if(carry>0)
pre->next = new ListNode(1);
pre = result->next;
delete result;
return pre;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
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) {
}
};
void CreateList_L(ListNode* &L,int n){
//带头结点尾插
L = (ListNode *)malloc(sizeof(ListNode));
L->next = NULL;
ListNode* m = L;
int e;
for (int i = n; i > 0;i--){
ListNode* p = (ListNode*)malloc(sizeof(ListNode));
scanf("%d", &e);
p->val = e;
m->next = p;
m = p;
}
m->next = NULL;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *result = new ListNode(-1);
ListNode *pre = result;
ListNode *pa = l1->next, *pb = l2->next;
int carry = 0;
while (pa != NULL || pb != NULL){
int av = pa == NULL ? 0 : pa->val;
int bv = pb == NULL ? 0 : pb->val;
cout << "av:" << av<<" bv: "<<bv<<endl;
ListNode *node = new ListNode((av + bv + carry) % 10);
carry = (av + bv + carry) / 10;
pre->next = node;
pre = pre->next;
pa = pa == NULL ? NULL : pa->next;
pb = pb == NULL ? NULL : pb->next;
}
if(carry>0)
pre->next = new ListNode(1);
pre = result->next;
delete result;
return pre;
}
void display_list(ListNode* head){
ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
cur = head;
cout << cur->val << " ";
while ((cur = cur -> next)){
printf("%d ", cur -> val);
}
free(cur);
printf("\n");
}
int main(){
ListNode* l1;
ListNode* l2;
CreateList_L(l1, 3);
CreateList_L(l2, 3);
ListNode* res=addTwoNumbers(l1, l2);
display_list(res);
return 0;
}
测试样例
input:
2 4 3
5 6 4
output:
7 0 8
模拟每一步操作
2 4 3
5 6 4
av:2 bv: 5
av:4 bv: 6
av:3 bv: 4
7 0 8