题解 | #牛牛的双链表求和#
牛牛的双链表求和
https://www.nowcoder.com/practice/efb8a1fe3d1f439691e326326f8f8c95
#include <stdio.h>
#include <stdlib.h>
//链表结点的定义
struct Node {
int num;
struct Node* Next;
};
//创建头结点
struct Node* creatList() {
struct Node* headnode;
headnode = (struct Node*)malloc(sizeof(struct Node));
headnode->num = 0;
headnode->Next = NULL;
return headnode;
}
//创建结点
struct Node* creatnode(int num) {
struct Node* newnode;
newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->num = num;
newnode->Next = NULL;
return newnode;
}
int main() {
int n, num;
scanf("%d\n", &n);
struct Node* a = creatList();
struct Node* q = a;
struct Node* p;
for (int i = 0; i < n; i++) {
scanf("%d ", &num);
p = creatnode(num);
//尾插法
q->Next = p;
q = p;
}
struct Node* b = creatList();
q = b;
for (int i = 0; i < n; i++) {
scanf("%d ", &num);
p = creatnode(num);
//尾插法
q->Next = p;
q = p;
}
for ( p = a->Next, q = b->Next; p != NULL ; p = p->Next, q = q->Next) {
printf("%d ", p->num + q->num);
}
return 0;
}

查看8道真题和解析