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