#include <stdio.h> #include <stdlib.h> //手撕链表 typedef int DateType; typedef struct List { DateType val; struct List* next; }LT; //返回新节点 LT* CreateBayNode(DateType x) { LT* newnode=(LT*)malloc(sizeof(LT)); if(newnode==NULL) { perror("newnode malloc fail"); return NULL; } newnode->next=NULL; newnode->val=x; return newnode; } //尾插 void PushBank(LT** head,DateType x) { //创建要插入的节点 LT* newnode = CreateBayNode(x); //判断头节点是否为NULL if(*head==NULL) { *head=newnode; } else { //进行找尾 LT* cur=*head; LT* tail=*head; while(cur) { tail=cur; cur=cur->next; } //进行连接 tail->next=newnode; } } //打印 void ListPrint(LT* head) { LT* cur=head; while(cur) { printf("%d ",cur->val); cur=cur->next; } } LT* SUM=NULL; void SumList(LT* A,LT* B) { LT* curA=A; LT* curB=B; while(curA && curB) { PushBank(&SUM, curA->val+curB->val); curA=curA->next; curB=curB->next; } } int main() { int n=0; scanf("%d",&n); int a[n]; int b[n]; LT* A=NULL; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<n;i++) { PushBank(&A, a[i]); } LT* B=NULL; for(int i=0;i<n;i++) { scanf("%d",&b[i]); } for(int i=0;i<n;i++) { PushBank(&B, b[i]); } //将两个链表求和并链接到另一个链表中 SumList(A,B); //打印 ListPrint(SUM); return 0; } //写出单链表结构,先将两个数组分别链接到两个链表中 //随后将两个链表值的和链接到一个新链表中 //最后将新链表打印出来