题解 | #牛牛的双链表求和#

牛牛的双链表求和

https://www.nowcoder.com/practice/efb8a1fe3d1f439691e326326f8f8c95

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node* next;
} node;
int main() {
    int n;
    scanf("%d", &n);
    node* a = malloc(sizeof(node)), *t1 = a;
    node* b = malloc(sizeof(node)), *t2 = b, *t3 = b;
    for (int i = 0; i < n * 2; i++) {
        node* t = malloc(sizeof(node));
        scanf("%d", &t->data);
        if (i < n) {
            t1->next = t;
            t1 = t;
        } else {
            t2->next = t;
            t2 = t;
        }
    }
    while (n--) {
        t3->next->data += a->next->data;
        t3 = t3->next;
        a = a->next;
    }
    while (b->next) {
        printf("%d ", b->next->data);
        b = b->next;
    }
    return 0;
}

#C#
0基础学C 文章被收录于专栏

0基础学C,从算法开始

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务