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

牛牛的单链表求和

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

#include <iostream>
using namespace std;
struct Node
{
    int num;
    Node* next;
};

void bulid_link_list(Node* dummy_head, int n, int* array)
{
    Node* temp = new Node;
    temp =  dummy_head;
    for(int i=0;i < n;i++)
    {
        Node* cur = new Node;
        temp->next = cur;
        cur->num = array[i];
        temp = temp->next;
    }
}

void print(Node* dummy_head, int n)
{
    int sum = 0;
    Node* temp = new Node;
    temp = dummy_head;
    for (int i = 0; i < n; i++)
    {
        temp = temp->next;
        sum += temp->num;
    }
    cout << sum;
}

int main()
{
    int n;
    cin >> n;
    int array[100];
    for (int i = 0; i < n; i++)
    {
        cin >> array[i];
    }
    Node* dummy_head = new Node;
    bulid_link_list(dummy_head, n, array);
    print(dummy_head, n);
    return 0;
}

全部评论

相关推荐

03-10 21:11
武汉大学 运营
学不懂的那种:先天考公圣体
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务