题解 | #两个链表生成相加链表#
两个链表生成相加链表
http://www.nowcoder.com/practice/2d4ae9ef94c8412ebe49118f8e1da2df
# 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* reverse(list_node*head)
{
if(head==NULL||head->next==NULL)
return head;
list_node*pre=NULL,*cur=head,*next=NULL;
while(cur){
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
list_node * add_list(list_node * head1, list_node * head2)
{
//////在下面完成代码
//大整数求和算法
//首先逆序这两个链表
list_node*newHead=NULL,*cur=NULL;
head1=reverse(head1);
head2=reverse(head2);
int flag=0;//进位
while(head1&&head2){
int data=(head1->val+head2->val+flag)%10;
flag=(head1->val+head2->val+flag)/10;
list_node*s=new list_node;
s->next=NULL;
s->val=data;
if(newHead==NULL){
newHead=s;
cur=s;
}
else{
cur->next=s;
cur=cur->next;
}
head1=head1->next;
head2=head2->next;
}
while(head1){
int data=(head1->val+flag)%10;
flag=(head1->val+flag)/10;
list_node*s=new list_node;
s->next=NULL;
s->val=data;
if(newHead==NULL){
newHead=s;
cur=s;
}
else{
cur->next=s;
cur=cur->next;
}
head1=head1->next;
}
while(head2){
int data=(head2->val+flag)%10;
flag=(head2->val+flag)/10;
list_node*s=new list_node;
s->next=NULL;
s->val=data;
if(newHead==NULL){
newHead=s;
cur=s;
}
else{
cur->next=s;
cur=cur->next;
}
head2=head2->next;
}
if(flag==1){
list_node*s=new list_node;
s->val=1;
s->next=NULL;
cur->next=s;
}
newHead=reverse(newHead);
return newHead;
}
void print_list(list_node * head)
{
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
puts("");
}
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;
}
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* reverse(list_node*head)
{
if(head==NULL||head->next==NULL)
return head;
list_node*pre=NULL,*cur=head,*next=NULL;
while(cur){
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
list_node * add_list(list_node * head1, list_node * head2)
{
//////在下面完成代码
//大整数求和算法
//首先逆序这两个链表
list_node*newHead=NULL,*cur=NULL;
head1=reverse(head1);
head2=reverse(head2);
int flag=0;//进位
while(head1&&head2){
int data=(head1->val+head2->val+flag)%10;
flag=(head1->val+head2->val+flag)/10;
list_node*s=new list_node;
s->next=NULL;
s->val=data;
if(newHead==NULL){
newHead=s;
cur=s;
}
else{
cur->next=s;
cur=cur->next;
}
head1=head1->next;
head2=head2->next;
}
while(head1){
int data=(head1->val+flag)%10;
flag=(head1->val+flag)/10;
list_node*s=new list_node;
s->next=NULL;
s->val=data;
if(newHead==NULL){
newHead=s;
cur=s;
}
else{
cur->next=s;
cur=cur->next;
}
head1=head1->next;
}
while(head2){
int data=(head2->val+flag)%10;
flag=(head2->val+flag)/10;
list_node*s=new list_node;
s->next=NULL;
s->val=data;
if(newHead==NULL){
newHead=s;
cur=s;
}
else{
cur->next=s;
cur=cur->next;
}
head2=head2->next;
}
if(flag==1){
list_node*s=new list_node;
s->val=1;
s->next=NULL;
cur->next=s;
}
newHead=reverse(newHead);
return newHead;
}
void print_list(list_node * head)
{
while (head != NULL) {
printf("%d ", head->val);
head = head->next;
}
puts("");
}
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;
}