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

牛牛的双链表求和

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

#include<stdio.h>
#include<stdlib.h>
typedef struct num
{
    int num;
    struct num *next;
}link;
link* create(int n,int a[])
{
    link* head,*now,*node;
    head =(link*)malloc(sizeof(link));
    now=head;
    for(int i=0;i<n;i++)
    {
        node =(link*)malloc(sizeof(link));
        node->num =a[i];
        now->next=node;
        now =node;
    }
    return head;
}
void add_(int n,link* a,link* b)
{
    int k;
    link*now,*new,*node,*nope;
    node =a->next;
    nope =b->next;
    for(int i=1;i<=n;i++)
    {
        now =node;
        new =nope;
        now->num+=new->num;
        node=node->next;
        nope=nope->next;
    }
    now=a->next;
    while(now!=NULL)
    {
        printf("%d ",now->num);
        now =now->next;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    int a[n],b[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int j=0;j<n;j++)
    {
        scanf("%d",&b[j]);
    }
    link*c =create(n,a);
    link*d= create(n,b);
    add_(n,c,d);
    return 0;
    
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务