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

牛牛的单链表求和

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

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

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

int main() {
    int count, value;

    scanf("%d", &count);

    Node *head = (Node*)malloc(sizeof(Node));
    Node *current = head;

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

    current = head;

    int result = 0;
    while (current->next != NULL) {
        result += current->data;
        current = current->next;
    }

    printf("%d", result);

    return 0;
}

全部评论

相关推荐

09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务