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

牛牛的单链表求和

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

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

typedef struct Node
{
    int num;
    struct Node *next;    
}LinkNode;

int main(int argc, char const *argv[])
{
    int n,i;
    scanf("%d", &n);
    LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode));
    LinkNode *tail;
    head->next = NULL;
    scanf("%d", &head->num);
    tail = head;
    for (i = 0; i < n-1; ++i)
    {
        LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode));
        scanf("%d", &p->num);
        p->next = NULL;
        tail->next = p;
        tail = tail->next;
    }
    tail = head;
    int max = 0;
    while(tail != NULL){
        max += tail->num;
        tail = tail->next;
    }
    printf("%d", max);
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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