题解 | #牛牛的双链表求和#
牛牛的双链表求和
https://www.nowcoder.com/practice/efb8a1fe3d1f439691e326326f8f8c95
#include <stdio.h>
typedef struct Node {
int data;
struct node* next;
} node;
node* cai(int v) {
node* kk = (node*)malloc(sizeof(node));
kk->data = v;
kk->next = NULL;
return kk;
}
int main() {
int n;
scanf("%d", &n);
node* head1 = NULL;
node* tail1 = NULL;
node* head2 = NULL;
node* tail2 = NULL;
for (int i = 0; i < n; i++) {
int o;
scanf("%d", &o);
node* kk = cai(o);
if (head1 == NULL) {
head1 = kk;
tail1 = kk;
} else {
tail1->next = kk;
tail1 = kk;
}
}
for (int i = 0; i < n; i++) {
int o;
scanf("%d", &o);
node* kk = cai(o);
if (head2 == NULL) {
head2 = kk;
tail2 = kk;
} else {
tail2->next = kk;
tail2 = kk;
}
}
node* p=head1;
node* p1=head2;
while(p1!=NULL){
p1->data=p1->data+p->data;
p=p->next;
p1=p1->next;
}
p1=head2;
while(p1!=NULL){
printf("%d ",p1->data);
p1=p1->next;
}
}
