题解 | #牛牛的单链表求和#
牛牛的单链表求和
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; }