两个链表生成相加链表
题目描述
假设链表中每一个节点的值都在 0 - 9 之间,那么链表整体就可以代表一个整数。
给定两个这种链表,请生成代表两个整数相加值的结果链表。
例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。
输入描述:
第一行两个整数 n 和 m,分别表示两个链表的长度。
第二行 n 个整数 a i a_i ai 表示第一个链表的节点。
第三行 m 个整数 b i b_i bi 表示第二个链表的节点。
输出描述:
输出一行整数表示结果链表。
示例1
输入
3 2
9 3 7
6 3
输出
1 0 0 0
备注:
1 ≤ n , m ≤ 1 0 6 1 \leq n,m \leq 10^6 1≤n,m≤106
0 ≤ a i , b i ≤ 9 0 \leq a_i,b_i \leq 9 0≤ai,bi≤9
a 1 , b 1 ! = 0 a_1,b1 != 0 a1,b1!=0
题解:
大数加法,在链表上操作而已。。。
法一:从头到尾遍历链表,将数据保存在栈里(相当于逆序),然后从栈顶到栈底遍历相加(相当于从低位到高位相加),注意一下进位就行。
法一代码:
# include <bits/stdc++.h>
using namespace std;
struct list_node{
int val;
struct list_node * next;
};
list_node * input_list(int n)
{
int val;
list_node * phead = new list_node();
list_node * cur_pnode = phead;
for (int i = 1; i <= n; ++i) {
scanf("%d", &val);
if (i == 1) {
cur_pnode->val = val;
cur_pnode->next = NULL;
}
else {
list_node * new_pnode = new list_node();
new_pnode->val = val;
new_pnode->next = NULL;
cur_pnode->next = new_pnode;
cur_pnode = new_pnode;
}
}
return phead;
}
list_node * add_list(list_node * head1, list_node * head2)
{
//在下面完成代码
if (!head1) return head2;
if (!head2) return head1;
vector<int> stk1;
vector<int> stk2;
int n1 = 0, n2 = 0;
list_node *p = head1;
while (p) {
stk1.push_back(p->val);
++n1;
p = p->next;
}
p = head2;
while (p) {
stk2.push_back(p->val);
++n2;
p = p->next;
}
--n1, --n2;
int a, b, c = 0;
list_node *now = NULL, *last = NULL;
while (n1 >= 0 || n2 >= 0) {
a = n1 >= 0 ? stk1[n1--] : 0;
b = n2 >= 0 ? stk2[n2--] : 0;
c += a + b;
now = new list_node();
now->val = c % 10;
now->next = last;
last = now;
c /= 10;
}
if (c) {
now = new list_node();
now->val = c;
now->next = last;
}
return now;
}
void reverse_list(list_node*& head) {
list_node* pre = NULL, *temp = NULL;
while (head) {
temp = head->next;
head->next = pre;
pre = head;
head = temp;
}
head = pre;
}
int main ()
{
int n, m;
scanf("%d%d", &n, &m);
list_node * head1 = input_list(n), * head2 = input_list(m);
list_node * new_head = add_list(head1, head2);
print_list(new_head);
return 0;
}
法二:将两个链表都逆序,然后相加就行,还是注意进位问题。
法二代码:
# include <bits/stdc++.h>
using namespace std;
struct list_node{
int val;
struct list_node * next;
};
list_node * input_list(int n)
{
int val;
list_node * phead = new list_node();
list_node * cur_pnode = phead;
for (int i = 1; i <= n; ++i) {
scanf("%d", &val);
if (i == 1) {
cur_pnode->val = val;
cur_pnode->next = NULL;
}
else {
list_node * new_pnode = new list_node();
new_pnode->val = val;
new_pnode->next = NULL;
cur_pnode->next = new_pnode;
cur_pnode = new_pnode;
}
}
return phead;
}
void print_list(list_node * head)
{
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
puts("");
}
void reverse_list(list_node*& head) {
list_node* pre = NULL, *temp = NULL;
while (head) {
temp = head->next;
head->next = pre;
pre = head;
head = temp;
}
head = pre;
}
list_node * add_list2(list_node * head1, list_node * head2) {
//在下面完成代码
if (!head1) return head2;
if (!head2) return head1;
reverse_list(head1);
reverse_list(head2);
int a, b, c = 0;
list_node *now = NULL, *last = NULL;
list_node *p1 = head1, *p2 = head2;
while (p1 || p2) {
a = p1 ? p1->val : 0;
b = p2 ? p2->val : 0;
c += a + b;
now = new list_node();
now->val = c % 10;
now->next = last;
last = now;
c /= 10;
p1 = p1 ? p1->next : NULL;
p2 = p2 ? p2->next : NULL;
}
if (c) {
now = new list_node();
now->val = c;
now->next = last;
}
reverse_list(head1);
reverse_list(head2);
return now;
}
int main ()
{
int n, m;
scanf("%d%d", &n, &m);
list_node * head1 = input_list(n), * head2 = input_list(m);
list_node * new_head = add_list2(head1, head2);
print_list(new_head);
return 0;
}