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

牛牛的双链表求和

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

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

typedef struct Node{
    int data;
    struct Node* next;
} Node;

int main(){
    int count, value;

    scanf("%d", &count);

    Node *head1 = (Node *) malloc(sizeof(Node));
    Node *head2 = (Node *) malloc(sizeof(Node));

    Node *current1 = head1;
    Node *current2 = head2; 

    int i = 0;

    for (i = 0; i < count; i++)
    {
        scanf("%d", &value);
        current1->data = value;
        current1->next = (Node *) malloc(sizeof(Node));
        current1 = current1->next;
    }

    for (i = 0; i < count; i++)
    {
        scanf("%d", &value);
        current2->data = value;
        current2->next = (Node *) malloc(sizeof(Node));
        current2 = current2->next;
    }

    current1 = head1;
    current2 = head2;
    
    while (current1->next != NULL)
    {
        current2->data = current2->data + current1->data;
        current1 = current1->next;
        current2 = current2->next;
    }

    current1 = head1;
    current2 = head2;

    while(current2->next != NULL){
        printf("%d ", current2->data);
        current2 = current2->next;
    }   
}

全部评论

相关推荐

拉丁是我干掉的:把上海理工大学改成北京理工大学。成功率增加200%
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务